DBC text, state, and initial values

I am working with ASP.NET MVC3 using EF and Code First.

I am writing a simple sampler for practice. In my controller I have a pretty standard code bit:

[HttpPost] public ActionResult Edit(Issue issue) { if (ModelState.IsValid) { dbContext.Entry(issue).State = EntityState.Modified ..... } } 

Part of Question 1 I am trying to understand how dbcontext works - Before I set the state to dbContext.Entry (problem), I assume that my problem object is detached. As soon as I set the state to be changed, the object is attached - but to what? dbContext or database? I donโ€™t seem to understand what this means (attachment) really means?

Part of question 2 For argumentation, let's say I decided to set the Accepted field in my problem. Accepted is logical. I start by saying that this is a lie, I set it to true and in form. At the moment when my object is attached, what is the point of the OriginalValues โ€‹โ€‹collection? for example, if I set a breakpoint right after setting EntityState.Modified, but before calling SaveChanges () I can request

 db.Entry(issue).OriginalValues["Accepted"] 

and this will give me the same meaning as just querying the problem object that was passed to Edit .... i.e. it gives the same result as

 issue.Accepted 

I am clearly missing something because the documentation says "The initial values โ€‹โ€‹are usually the values โ€‹โ€‹of the properties of the object, as they were when the last query from the database." But this is not so, because the database still reports that it is accepted as false (yes, I noticed the word โ€œusuallyโ€ in the documents, but my code is pretty much standard, generated by the MS code, so ...). So what am I missing? what is really going on here?

+4
source share
1 answer

Context can only work with attached objects. Attachment means that the context knows about the object, it can save its data, and in some cases it can provide additional functions, such as change tracking or lazy loading.

By default, all objects loaded from the database by the context instance are attached to this instance. In the case of web applications and other disabled scripts, you have a new context instance for each processed HTTP request (if you do not, you make a big mistake). In addition, your object created using model binding in HTTP POST is not loaded by this context - it is disconnected. If you want to save this entity, you must attach it and report the changes that you have made. The state value from Entry to Modified will perform both operations: it will attach the object to the context and set its global state to Modified , which means that all scalar and complex properties will be updated when SaveChanges called.

So, setting the state to Modified , you bound the object to the context, but until you call SaveChanges , it will not affect your database.

OriginalValues are mostly useful in fully attached scripts where you load an object from a database and make changes to this attached object. In this case, OriginalValues show the property values โ€‹โ€‹loaded from the database, and CurrentValues show the actual values โ€‹โ€‹set by your application. In the context of the scenario, the initial values โ€‹โ€‹are not known. He believes that the initial values โ€‹โ€‹are those that are used when attaching the object.

+3
source

All Articles