无法更新实体框架 6 中的外键

Unable to update Foreign Key in Entity Framework 6(无法更新实体框架 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 both Person.

    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 中的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)