无法更新实体框架中的外键6 [英] Unable to update Foreign Key in Entity Framework 6

查看:141
本文介绍了无法更新实体框架中的外键6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我使用的代码:

我正在尝试对外键进行简单的更新,但脚本永远不会被发送。

  using(var db = new MyContext())
{
db.Entry< Contact>(newContact) = EntityState.Modified;
newContact.ContactOwner = db.Person.Find(3);
db.SaveChanges();
}

EF6更新了Persons表中的其余列,但不更新Person表中的Contact_Id。



个人实体:

  public class Person 
{
public int Id {get;组; }
public string Name {get;组; }
public List< Contact> ContactList {get;组; }
}

联系实体:



X-45454545 X- 20045 X-454545 X-454545 X-454545 X- 20045 CEEC X-组; }
public string Email {get;组; }
public string TelNo {get;组; }
public Person ContactOwner {get;组;
}

我在这里缺少什么?



请帮助!

解决方案

由于您正在与独立协会合作。您可以




  • ContactList 中添加和删除关系,但您需要从 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();


  • 或者您可以使用断开连接的对象,但需要手动管理关系。 p>

      db.Entry(newContact).State = EntityState.Modified; 

    var p1 = new Person {Id = 1}; X-454545 X-454545 X-454545 X- 20045 X-4545 X- 20045 X- 20045 X- 20045 X-4545 X-4545
    var p3 = new Person {Id = 3}; CE X- 20045 X-454545 X-454545 X- 20045 X-454545 X- 20045 X- 20045 X-4545 X- 20045 X-454545

    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 / p>

您可能需要重新考虑添加外键值,以便使一切更容易,只需提及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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆