如何在实体框架中更新导航属性? [英] How do you update navigation properties in entity framework?

查看:63
本文介绍了如何在实体框架中更新导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试同时更新数据库中的两个对象.使用我当前的代码,它似乎只更新了我的导航属性.例如,如果我的第一堂课中的分数"发生变化,它将不会被更新.但是,如果第二类中的任何内容发生更改,它将正确更新.我不确定在这里我做错了什么.这是代码优先的实体框架.

I am trying to update two objects in a database simultaneously. With my current code here it only seems to update my navigation property. For instance if the "Score" in my first class changes it will not be updated. But if anything in the second class changes it will update properly. I am not sure what I am doing wrong here. This is code-first entity framework.

我有两节课:

public class SampleClass
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public int TestId { get; set; }
    public string Items { get; set; }
    public int Score { get; set; }

    public IList<Test> Test { get; set; }
}

还有另一个看起来像这样的类:

And another class that looks like this:

public class Test
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int TestId { get; set; }
    public double RawScore { get; set; }
    public double PercentScore { get; set; }

    public int SampleClassId { get; set; }

    [ForeignKey("SampleClassId")]
    public virtual SampleClass SampleClass { get; set; }
}

要在数据库中进行更新,我需要这样做:

To update in the database I have this:

    var entry = context.Entry(sampleFormClass);
    entry.State = System.Data.Entity.EntityState.Modified;
    entry.Property(e => e.Items).IsModified = false;

    foreach (var navigationProperty in myTestClass.Test)
    {
           var entityEntry = context.Entry(navigationProperty);
           entityEntry.State = System.Data.Entity.EntityState.Modified;
           entityEntry.Property(navProp => navProp.SampleClassId).IsModified = false;
    }

    context.FormData.Attach(sampleFormClass);
    }
    context.SaveChanges();

如果我在更新对象的方法中删除了该foreach,则它将不会更新我的nav属性.如何同时更新我的​​对象及其对象列表?

If I remove that foreach in my method to update the object then it will not update my nav property. How can I update both my object and its list of objects at the same time?

解决我的问题

编辑:我可以通过将更新方法中的代码重新安排为自己来解决自己的问题:

Edit: I was able to fix my own problem by rearranging the code in my update method to this:

    context.FormData.Attach(sampleFormClass);

    var entry = context.Entry(sampleFormClass);
    entry.State = System.Data.Entity.EntityState.Modified;
    entry.Property(e => e.Items).IsModified = false;

    foreach (var navigationProperty in myTestClass.Test)
    {
           var entityEntry = context.Entry(navigationProperty);
           entityEntry.State = System.Data.Entity.EntityState.Modified;
           entityEntry.Property(navProp => navProp.SampleClassId).IsModified = false;
    }


    }
    context.SaveChanges();

我刚刚将要附加对象的行移到了要更新的其他地方.

I just moved the line where you are attaching the object you are going to update above everything else.

推荐答案

我能够通过将更新方法中的代码重新安排为自己来解决自己的问题:

I was able to fix my own problem by rearranging the code in my update method to this:

context.FormData.Attach(sampleFormClass);

var entry = context.Entry(sampleFormClass);
entry.State = System.Data.Entity.EntityState.Modified;
entry.Property(e => e.Items).IsModified = false;

foreach (var navigationProperty in myTestClass.Test)
{
       var entityEntry = context.Entry(navigationProperty);
       entityEntry.State = System.Data.Entity.EntityState.Modified;
       entityEntry.Property(navProp => navProp.SampleClassId).IsModified = false;
}


}
context.SaveChanges();

我刚刚将要附加对象的行移到了要更新的其他地方.

I just moved the line where you are attaching the object you are going to update above everything else.

这篇关于如何在实体框架中更新导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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