EF:对象更新过程不会更改一个属性的值

时间:2015-07-12 23:29:56

标签: c# entity-framework

我的应用程序有两个类:PaymentMethod& CurrencyCurrencyPaymentMethod)的财产。当我的应用更新PaymentMethod时,新值Currency属性(值已存在于数据库中,但已分配给PaymentMethod),在SaveCHanges方法之后,{{1属性仍包含旧值。的 WHY:?)

这是我的应用程序替换货币对象值的方式:

Currency

if (String.Compare(existingPaymentMethod.Currency.Code, mwbepaymentmethod.CurrencyCode, true) !=0) { var readCurrency = currencyRepo.FindByCode(mwbepaymentmethod.CurrencyCode); existingPaymentMethod.Currency = readCurrency; } paymentMethodRepository.Save(ref existingPaymentMethod); return true; & PaymentMethod课程:

Currency

编辑方法:

public class PaymentMethod : BaseEntity
    {
        public enum MethodTypeEnum
        {
            Creditcard,
            Virtualcard,
            Wallet
        };
        public MethodTypeEnum MethodType { get; set; }
        public int VendorId { get; set; }
        public virtual Address BillingAddress { get; set; }
        public virtual Currency Currency { get; set; }
    }

public class Currency : BaseEntity
    {
        [JsonProperty("code")]
        [Key]
        public string Code { get; set; }

        [JsonProperty("symbol")]
        public string Symbol { get; set; }

        [JsonIgnore]
        public virtual ICollection<Payment> Payments { get; set; }

        [JsonIgnore]
        public virtual ICollection<PaymentMethod> PaymentMethods { get; set; }
    }

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

OnModelCreating

Autofac定义的DB上下文:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MobileWalletContext>());
           ...
            modelBuilder.Entity<MwbePaymentMethod>().HasRequired(e => e.Currency).WithMany(e => e.PaymentMethods);

            base.OnModelCreating(modelBuilder);
        } 

更新1 : EF日志显示没有货币字段更新:

builder.RegisterType<MobileWalletContext>().As<IMwbeDbContext>().InstancePerRequest();

为什么UPDATE [dbo].[MwbeAddress] SET [Street] = @0, [City] = @1, [ZipCode] = @2, [Country] = @3 WHERE ([Id] = @4) -- @0: 'FFFF12' (Type = String, Size = -1) -- @1: 'GlasgowSSSS' (Type = String, Size = -1) -- @2: 'B33 8TH' (Type = String, Size = -1) -- @3: 'England' (Type = String, Size = -1) -- @4: '2' (Type = Int32) -- Executing at 2015-07-13 07:35:48 +02:00 -- Completed in 39 ms with result: 1 UPDATE [dbo].[MwbePaymentMethod] SET [MethodType] = @0, [VendorId] = @1, [Number] = @2, [ExpirationDate] = @3, [Balance] = @4, [IsPending] = @5, [IsDefault] = @6 WHERE ([Id] = @7) -- @0: '1' (Type = Int32) -- @1: '0' (Type = Int32) -- @2: '4444 4444 4444 4450' (Type = String, Size = -1) -- @3: '2015-10-10 00:00:00' (Type = DateTime2) -- @4: '0' (Type = Double) -- @5: 'True' (Type = Boolean) -- @6: 'False' (Type = Boolean) -- @7: '3' (Type = Int32) -- Executing at 2015-07-13 07:35:48 +02:00 -- Completed in 7 ms with result: 1 属性没有更新?

3 个答案:

答案 0 :(得分:9)

设置实体的状态(Added除外)仅影响实体的标量属性,而不影响其导航属性,关联

所以你有三个选择:

选项1

将货币附加到上下文。在Edit方法中:

Context.Entry(entityToUpdate).State = EntityState.Modified;
Context.Entry(entityToUpdate.Currency).State = EntityState.Unchanged;

现在EF知道分配给Currency的{​​{1}},因此它知道关联已更改,并且它将更新数据库中的外键。

但我不认为这对你有用。从您的问题陈述中我了解PaymentMethodcurrencyRepo不会共享相同的背景,否则您首先不会遇到问题(货币已经附加了)。您无法将实体附加到两个上下文,因此应该在该点处置paymentMethodRepository的上下文,或者您应该首先从中分离货币。非常费力。

选项2

currencyRepocurrencyRepo(以及所有相关的存储库)在一个工作单元内共享相同的上下文实例。无论如何,这是推荐的,不仅是为了解决这个问题。

选项3

不要设置paymentMethodRepository属性,但添加原始外键属性Currency并在货币更改时修改该属性。这是标量属性,因此它将响应设置PaymentMethod.CurrencyId

答案 1 :(得分:5)

DbSet.Attach不是递归的。您需要附加所有已发布的实体:

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;
        }

答案 2 :(得分:0)

此问题是由于在PaymentMethod类中未定义foreign key关系。需要指定这样的特定列将用于更新,如果货币是新的,那么将插入并且将相对于货币代码保存相同的代码。

public class PaymentMethod : BaseEntity
{
    public enum MethodTypeEnum
    {
        Creditcard,
        Virtualcard,
        Wallet
    };
    public MethodTypeEnum MethodType { get; set; }
    public int VendorId { get; set; }
    public virtual Address BillingAddress { get; set; }

    public string CurrencyCode {get;set;} //Replace with actual column name

     [ForeignKey("CurrencyCode ")]
    public virtual Currency Currency { get; set; }

}