EF 带外键关系更新实体问题

2020-08-17 14:46发布

问题:

排版原因写道博客里了,求大神康康

https://www.cnblogs.com/swobble/p/13503161.html

@会长 @爱编程的大叔 @花飘 and so on....

回答1:

https://stackoverflow.com/questions/25417849/unable-to-update-foreign-key-in-entity-framework-6

Basically this happens because EntryState.Modified just looks for scalar properties (primitive types) and with independent association (your case) you don't have it.

GraphDiff

using (var context = new Context())
{
var customer = new Customer()
{
Id = 12503,
Name = "Jhon Doe",
City = new City() { Id = 8, Name = "abc" }
};

context.UpdateGraph(customer, map => map.AssociatedEntity(p => p.City));
context.Configuration.AutoDetectChangesEnabled = true;

context.SaveChanges();

}



回答2:

我替你at去



回答3:

好久没用了,我有空试试,你用版本是什么?