我有两个表:
客户(id,客户名) 项目(id, clientid, projecttitle)外键引用clientid -> clients.id
当我像这样使用 EF 加载项目时:
thisProject = (from p in dataEntity.projects.Include("client")
where p.id == INTVALUE
select p).FirstOrDefault();
然后我更改了 thisProject 中的一些值,并想更改与不同客户的关系,它不允许我修改 Projects 表中的 clientid 字段。
希望我解释得足够好,谢谢
最佳答案
你需要做这样的事情:
var thisProject = (from p in dataEntity.projects.Include("client")
where p.id == INTVALUE
select p).FirstOrDefault();
var newClient = dataEntity.clients.FirstOrDefault(); // change to suit
thisProject.Client = newClient;
您不能只更改 EntityKey AFAIK - 您需要实际更改导航引用。
HTH.
关于C# Entity Framework : How do I update a record and change foreign key reference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4181928/