Entity framework, problems updating related objects

前端 未结 3 1310
野性不改
野性不改 2020-11-30 18:23

I am currently working on a project using the latest version of Entity Framework and I have come across an issue which I can not seem to solve.

When it comes to up

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 18:32

    You can also call the tables independently

    MyContext db = new MyContext
    // I like using asynchronous calls in my API methods 
    var OldFoo = await db.Foo.FindAsync(id);
    var OldAssociateFoo = db.AssociatedFoo;  
    var NewFoo = OldFoo;
    var NewAssociatedFoo = OldAssociatedFoo;
    
    NewFoo.SomeValue = "The Value";
    NewAssociatedFoo.OtherValue = 20;
    
    db.Entry(OldFoo).CurrentValues.SetValues(NewFoo);
    db.Entry(OldAssociatedFoo).CurrentValues.SetValues(NewAssociatedFoo);
    
    await db.SaveChangesAsync();
    

提交回复
热议问题