Entity Framework 4.1 - default EntityState for FK?

I have a little problem with ASP.NET MVC and Entity Framework 4. I have an object called "UF" and the other is "Pais" and they have this relationship:

UF [* ... 0..1] Pais 

I can access the Pais object directly from UF using the navigation property:

 UF.Pais.Whatever = "foobar"; 

I currently have a view that inserts a new item into the database, and it has an editor for "Pais.Codigo" ("Codigo" is the main key for Pais). Therefore, when I insert a new UF, the environment creates an instance of the UF class with reference to an instance of the Pais class. Then it is done:

 if (ModelState.IsValid) { db.UFs.AddObject(uf); db.SaveChanges(); return View(); } 

The problem is that EF inserts the new Pais into the database, so it basically ignores the existing one.

For example, if my UF object has Pais with identifier 1. The current value of uf.Pais.Codigo is 1. Other attributes, such as the description, are currently zero. When I execute SaveChanges, both "uf" and "uf.Pais" have an "Added" state. The correct state for "uf.Pais" must be unchanged since it already exists in the database.

My question is: is there a way to change the default relationship of EntityState for Unchanged? The following code solves the problem, but adding it to each function with adding a new record to the database (and for each FK) is redundant!

 db.ObjectStateManager.ChangeObjectState(uf.Pais, EntityState.Unchanged); 

What is it. I'm not sure if I was clear enough. Feel free to ask for more information if necessary. And sorry for any English mistakes!

Thanks,

Ricardo

PS: "Pais" stands for country and "UF" for state.

0
source share
1 answer

My question is: is there some way to change the default relationship of EntityState for Unchanged ?

Yes, calling Attach instead of Unchanged .

The following code solves the problem, but adds it to each function with the addition of a new record in the database (and for each FK) is redundant!

No, this is not a surplus, it is a solution, because either Attach or AddObject will always perform an operation for all entities and associations in the entity graph. This means that calling AddObject will do everything that the context does not know yet, since Added and Attach will do everything that the context does not know as Unchanged (so you, in turn, will have to set each changed or inserted object to the correct state) . This is how EF works if you use separate objects.

Another solution to the problem is to connect after adding the UF :

 // Here UF.Pais is null db.UFs.AddObject(uf); // Create dummy Pais var pais = new Pais { Id = "Codigo" }; // Make context aware of Pais db.Pais.Attach(pais); // Now make the relation uf.Pais = pais; db.SaveChanges(); 

If you work with individual objects, you are always responsible for setting the correct state for each object (and independent association ). This way you will use attached objects so that EF can do the magic for you (as shown in the example), or you will use an approach that you don't like. In more complex scenarios, you may find out that the best approach is to load the object graph again and merge the incoming changes into the attached graph.

0
source

All Articles