简体   繁体   English

在EF6中更新外键对象

[英]Update Foreign Key Object in EF6

I have following Models: 我有以下型号:

public class DivorceCases
{
  [Key]
  [Required]
  public string case_id { get; set; }
  public string archived { get; set; }
  public virtual Plaintiff p { get; set; }
}

public class Plaintiff{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
    public string name { get; set; }
}

I am having a ModelView of DivorceCases for editing and in controller, I am using: 我正在使用DivorceCases的ModelView进行编辑,并且在控制器中,我正在使用:

DivorceCases dcold = db.DivorceCase.Where(x => x.case_id == dc.case_id).Include(x => x.p).SingleOrDefault();
            dcold.p = dc.p;
            db.Entry(dcold).State = EntityState.Modified;
            db.SaveChanges();

When I update, instead of Updating the existing entry in Plaintiffs table, the EF6 inserts a new record in Plaintiffs table and updates the foreign key reference for this new record in DivorceCases table. 当我更新时,EF6不会更新原告表中的现有条目,而是在原告表中插入一条新记录,并在DivorceCases表中更新此新记录的外键引用。 What am I doing wrong? 我究竟做错了什么? How do I get rid of that? 我该如何摆脱呢?

You are asking your attached case entity to link to an item that is not attached to the context. 您正在要求附加的案例实体链接到附加到上下文的项目。

Try to attach dc.p first or simply change the plaintiff id on the case instead of changing object. 尝试先附加dc.p,或者简单地更改案例的原告ID而不是更改对象。

Instead of assigning dc.p to dcold.p assign the properties of dc.p to dcold.p 's properties then call SaveChanges(); 而不是分配dc.pdcold.p分配的属性dc.pdcold.p的属性然后调用SaveChanges(); . you don't need db.Entry(dcold).State = EntityState.Modified; 您不需要db.Entry(dcold).State = EntityState.Modified; entity framework will update your data instead of creating new entry. 实体框架将更新您的数据,而不是创建新条目。 Try the following code- 尝试以下代码-

    DivorceCases dcold = db.DivorceCase.Where(x => x.case_id == dc.case_id)
            .Include(x => x.p).SingleOrDefault();
            dcold.p.name = dc.p.name;
            db.SaveChanges();

I got a solution after repeated debugging and hit and trials. 经过反复调试和试用,我得到了解决方案。

First of all I made a DBContextExtension Class like this: 首先,我制作了一个DBContextExtension类,如下所示:

public static T Modify<T>(this T t,T tnew)
    {
        PropertyInfo[] properties = typeof(T).GetProperties();
        foreach (PropertyInfo property in properties)
        {
            if (property.Name != "id" && (property.PropertyType==typeof(string) || property.PropertyType==typeof(DateTime)))
            {
                if (property.GetValue(tnew) != null)
                property.SetValue(t, property.GetValue(tnew));
            }
        }
        return t;
    }

Then for each foreign key referenced object, I called this Extension method and got 100% success. 然后,对于每个外键引用的对象,我都调用了此Extension方法,并获得了100%的成功。

DivorceCases dcold = db.DivorceCase.Where(x => x.case_id == dc.case_id).Include(x => x.p).SingleOrDefault();
            dcold = dcold.Modify(dc);
            dcold.p = dcold.p.Modify(dc.p);
            dcold.d = dcold.d.Modify(dc.d);
            db.SaveChanges();

maybe try this before your db.SaveChanges() call.. 也许在您的db.SaveChanges()调用之前尝试一下。

 db.p=db.Plaintiff.Where(x=>x.id==db.p.id);

Not sure if that'll work here. 不确定是否可以在这里工作。 My database structure is slightly different. 我的数据库结构略有不同。 I'm storing foreign key ids in the linking table... I use that value passed in... (...Where(x=>x.id==PlaintiffID) 我将外键ID存储在链接表中...我使用传入的值...(...在哪里(x => x.id == PlaintiffID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM