EF: object update process is not changing value of one property

后端 未结 3 1413
一个人的身影
一个人的身影 2021-01-17 16:50

my application has 2 classes: PaymentMethod & Currency (Currency is property of PaymentMethod). When my app does upda

3条回答
  •  一向
    一向 (楼主)
    2021-01-17 17:40

    DbSet.Attach is not recursive. You need to attach all the entities involed:

    public override void Edit(MwbePaymentMethod entityToUpdate)
            {
                DbSet.Attach(entityToUpdate);
                Context.Entry(entityToUpdate).State = EntityState.Modified;
    
                if(entityToUpdate.BillingAddress != null)
                {
                  DbSet.Attach(entityToUpdate.BillingAddress);
                  Context.Entry(entityToUpdate.BillingAddress).State = EntityState.Modified;
                }
    
                if(entityToUpdate.Currency != null)
                {
                  DbSet.Attach(entityToUpdate.Currency);
                  Context.Entry(entityToUpdate.Currency).State = EntityState.Modified;
                }
    
                //manual update of  properties
                //Context.Entry(entityToUpdate.BillingAddress).State = EntityState.Modified;
                //Context.Entry(entityToUpdate.Currency).State = EntityState.Unchanged;
            }
    

提交回复
热议问题