When a call is made to the SaveChanges() method of an context, the relationships on the other side of the change are automatically updated.
For instance if I had a teacher mrX with a virtual ICollection of students including littleJohnny
mrX.Students.Remove(littleJohnny);
Debug.Assert(littleJohnny.Teacher!=null); //assert should pass
context.SaveChanges();
Debug.Assert(littleJohnny.Teacher==null); //assert should pass
mrX.Students.Add(littleJohnny);
context.SaveChanges();//revert to previous state
littleJohnny.Teacher=null;
Debug.Assert(mrX.Students.Contains(littleJohnny)); //assert should pass
context.SaveChanges();
Debug.Assert(!mrX.Students.Contains(littleJohnny)); //assert should pass
Is there any way to update such relationships without saving the data to the database in Entity Framework 4.3 and 5.0 ?
In a different scenario, If I have a ViewModel which maps to the above entities, is there a simple way I can copy this EF behavior -> that is, track the relationships and update the relationships on calling a method?
try calling context.ChangeTracker.DetectChanges()
if that doesn't work then your related entities have not been initialised correctly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.