Unable to update Foreign Key in Entity Framework 6(无法更新实体框架 6 中的外键)
问题描述
我正在尝试对外键进行简单更新,但脚本从未发送过来.
这是我正在使用的代码:
使用 (var db = new MyContext()){db.Entry<Contact>(newContact).State = EntityState.Modified;newContact.ContactOwner = db.Person.Find(3);db.SaveChanges();}
EF6 更新 Persons 表中的其余列,但不更新 Persons 表中的 Contact_Id.
个人实体:
公共类Person{公共 int ID { 获取;放;}公共字符串名称 { 获取;放;}公开名单<联系方式>联系人列表 { 获取;放;}}
联系实体:
公开课联系方式{公共 int ID { 获取;放;}公共字符串电子邮件{获取;放;}公共字符串 TelNo { 获取;放;}公共人员联系人所有者 { 获取;放;}}
我在这里错过了什么?
请帮忙!
因为您正在与独立协会合作.你可以
在
ContactList
中添加和删除关系,但您需要从两个Person
中检索.db.Entry(newContact).State = EntityState.Modified;var p1 = db.Set
().Include(p => p.ContactList).FirstOrDefault(p =>p.Id == 1);p1.ContactList.Remove(newContact);var p3 = db.Set ().Include(p => p.ContactList).FirstOrDefault(p => p.Id == 3);p3.ContactList.Add(newContact);db.SaveChanges(); 或者你可以使用断开的对象,但是你需要手动管理关系.
db.Entry(newContact).State = EntityState.Modified;var p1 = 新人 { Id = 1 };db.Entry(p1).State = EntityState.Unchanged;var p3 = 新人 { Id = 3 };db.Entry(p3).State = EntityState.Unchanged;var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager;manager.ChangeRelationshipState(newContact, p1, item => item.ContactOwner,实体状态.已删除);manager.ChangeRelationshipState(newContact, p3, item => item.ContactOwner,实体状态.添加);db.SaveChanges();
附言
您可能需要重新考虑添加外键值,以使一切变得更容易,只需提及 Id 即可更新外键.
有关详细信息,请参阅这篇文章.p>
I am trying to do a simple update to the foreign key but the script never get sent over.
Here is the code I am using:
using (var db = new MyContext())
{
db.Entry<Contact>(newContact).State = EntityState.Modified;
newContact.ContactOwner = db.Person.Find(3);
db.SaveChanges();
}
EF6 update the rest of the column in the Persons table but it is not updating the Contact_Id in Persons table.
Person entity:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public List<Contact> ContactList { get; set; }
}
Contact entity:
public class Contact
{
public int Id { get; set; }
public string Email { get; set; }
public string TelNo { get; set; }
public Person ContactOwner { get; set; }
}
What am I missing here?
Please help!
Since you are working with independent association. You can either
Adding and removing the relationship from
ContactList
, but you need to retrieve from bothPerson
.db.Entry(newContact).State = EntityState.Modified; var p1 = db.Set<Person>().Include(p => p.ContactList) .FirstOrDefault(p =>p.Id == 1); p1.ContactList.Remove(newContact); var p3 = db.Set<Person>().Include(p => p.ContactList) .FirstOrDefault(p => p.Id == 3); p3.ContactList.Add(newContact); db.SaveChanges();
Or you can use disconnected object, but you need to manually manage the relationship.
db.Entry(newContact).State = EntityState.Modified; var p1 = new Person { Id = 1 }; db.Entry(p1).State = EntityState.Unchanged; var p3 = new Person { Id = 3 }; db.Entry(p3).State = EntityState.Unchanged; var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager; manager.ChangeRelationshipState(newContact, p1, item => item.ContactOwner, EntityState.Deleted); manager.ChangeRelationshipState(newContact, p3, item => item.ContactOwner, EntityState.Added); db.SaveChanges();
PS
You might need to reconsider adding foreign key value, to make everything easier, updating foreign key just by mentioning the Id.
See this post for more information.
这篇关于无法更新实体框架 6 中的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法更新实体框架 6 中的外键
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01