且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在实体框架中更新导航属性?

更新时间:2023-02-13 10:39:19

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

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.