简体   繁体   中英

How do you update navigation properties in entity framework?

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.

I have two classes:

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();

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?

Solution to my problem

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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