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

2020-08-17 14:29发布

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

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

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

3条回答
聊天终结者
2楼-- · 2020-08-17 14:32

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

查看更多
太酷不给撩
3楼-- · 2020-08-17 14:48

我替你at去

查看更多
等我变得足够好
4楼-- · 2020-08-17 14:53

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();

}

查看更多
登录 后发表回答