C# EF:对象更新过程未更改一个属性的值
我的应用程序有两个类:C# EF:对象更新过程未更改一个属性的值,c#,entity-framework,C#,Entity Framework,我的应用程序有两个类:PaymentMethod和Currency(Currency是PaymentMethod的属性)。当我的应用程序使用Currencyproperty的新值更新PaymentMethod时(值已存在于数据库中,但它被分配给PaymentMethod),在SaveCHanges方法之后,Currency属性仍然包含旧值为什么?:) 以下是我的应用程序替换货币对象值的方式: if (String.Compare(existingPaymentMethod.Currency.C
PaymentMethod
和Currency
(Currency
是PaymentMethod
的属性)。当我的应用程序使用Currency
property的新值更新PaymentMethod
时(值已存在于数据库中,但它被分配给PaymentMethod
),在SaveCHanges
方法之后,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;
支付方法
和货币
类别:
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; }
}
关于模型创建
方法:
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);
}
Context.Entry(entityToUpdate).State = EntityState.Modified;
Context.Entry(entityToUpdate.Currency).State = EntityState.Unchanged;
为什么不更新
货币
属性?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;
}
设置实体的状态(添加的除外
)只会影响实体的标量属性,而不会影响其导航属性及其关联
所以你有三个选择:
选择1
将货币附加到上下文。在编辑方法中:
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);
}
Context.Entry(entityToUpdate).State = EntityState.Modified;
Context.Entry(entityToUpdate.Currency).State = EntityState.Unchanged;
现在EF知道分配给PaymentMethod
的货币
,因此它知道关联已更改,并将更新数据库中的外键
但我不认为这样对你有用。从您的问题陈述中,我了解到currencyRepo
和paymentMethodRepository
没有相同的上下文,否则您从一开始就不会有问题(货币已经附加)。您不能将一个实体附加到两个上下文,因此要么在该点处置currencyRepo
的上下文,要么首先将货币与其分离。相当辛苦
选择2
让currencyRepo
和paymentMethodRepository
(以及与此相关的所有存储库)在一个工作单元内共享相同的上下文实例。无论如何,建议这样做,不仅是为了解决这个问题
选择3
不要设置Currency
属性,而是添加一个基本的外键属性PaymentMethod.CurrencyId
,并在货币更改时修改该属性。这是一个标量属性,因此它将响应设置EntityState。已修改此问题是因为PaymentMethod类中未定义中的关系。需要指定,以便使用特定列进行更新,如果货币是新的,则将插入该列,并根据currencycode保存相同的代码
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; }
}
是否输入了if条件?是的,输入了if条件我将在5-6小时内给出答案,仍在等待其他解释;)。谢谢DbContext.Attach
不是found@P.K.oops已定义货币的外键,但名称为:FK_dbo.MwbePayment_dbo.MwbeCurrency_Currency_code。我在数据库中看到它。这也需要在类级别的代码中指定,无论是在ModelCreation还是在属性级别。在ModelCreation上,您指定了关系,但没有指定定义外键关系的属性(键)。请检查上面的代码如何指定外键关系。我没有实现这一点,但它似乎是合理的。我会检查一下,让你知道。
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; }
}