My question is directly in relation to this one.
The accepted answer says "You also have to change state of the relation".
I use Model-First approach, and I don't have the foreign key in my entity. I only have the navigation property.
I Can change the state of the entities via DbEntityEntry
, but I can't figure how to change the state of the relation itself. How can I access it?
Exemple of my code :
Building building = new Building() { Id = 1, Name = "modified" }; //Buiding 1 exists in DB
building.Adress = new Adress() { Id = 1, Road = "Sesame street" }; //Address 1 exists in DB
building.Adress.State = new State() { Id = 1 }; //State with Id 1 exists in DB
dbContext.Entry<Building>(building).State = EntityState.Modified;
dbContext.Entry<Adress>(building.Adress).State = EntityState.Modified;
//The state itself is not modified, but the relation between adress and state may do.
dbContext.Entry<State>(building.Adress.State).State = EntityState.Unchanged;
dbContext.SaveChanges();
This code pass without error, but the relation between Adress
and State
is never updated.
The Name
of the building and the Street
properties does.