What really happens when a model goes out of scope?

I use db models using blocks so as not to be confused with utilities and any unmanaged resource requirements associated with them, but I want to know (mainly) what happens when the db model goes out of scope. The issue is not recycling or memory (I know he does it automatically for us). Question about connecting SQL and related instances.

So here is a simple example ( ActionResult ):

 Account account; using(AccountsModel accountModel = new AccountsModel()) { account = accountsModel.Accounts.FirstOrDefault(x=> x.Username == username); if(account == null) return; account.Name = name; accountsModel.SaveChanges(); } ViewBag.Name = account.Name; // Is it safe? return View(); 

The question arises: "Is it safe to access an instance of the Account class (which is provided by AccountsModel ) from a model using a scope?"

Note: I know that I could not / could not / could not perform any update in the table out of scope.

+5
source share
1 answer

As long as the call to AccountsModel dispose does nothing to destroy it, then there should be no problem continuing access to the object created by the model.

So it really depends on the dispose implementation

0
source

All Articles