【发布时间】:2017-03-01 17:13:25
【问题描述】:
我在我的应用程序中使用代码优先实体框架(代码优先)。
当我尝试添加新对象并将其保存到数据库时,EF 抛出此错误。
属性“AccountNumber”是对象的关键信息的一部分 并且不能修改。
关于这个错误的第一件令人困惑的事情是 AccountNumber 不是该表中的主键。
在阅读了几篇文章来解决这个问题后,我尝试在它保存之前创建一个全新的对象:
public void CreateCustomerLookUp(CustomerLookUp customerLookUp)
{
//To avoid EF specific error recreating object using NEW - wierd!
//http://stackoverflow.com/questions/3187963/the-property-id-is-part-of-the-objects-key-information-and-cannot-be-modified
customerLookUp.LastUpdated = DateTime.Now;
var encryptedCustomerLookUp = EncryptCustomerLookUp(customerLookUp);
var newCustomerLookup = new CustomerLookUp()
{
AccountNumber = encryptedCustomerLookUp.AccountNumber,
EmailAddress = encryptedCustomerLookUp.EmailAddress,
MobileNumber = encryptedCustomerLookUp.MobileNumber,
UmbracoMemberId = encryptedCustomerLookUp.UmbracoMemberId,
UpdateCode = encryptedCustomerLookUp.UpdateCode,
UpdateNotification = encryptedCustomerLookUp.UpdateNotification
};
customerDataContext.Entry(newCustomerLookup).State = EntityState.Added;//<< Throwing error here!
customerDataContext.SaveChanges();
}
我也试过
customerDataContext.CustomerLookUps.Add(newCustomerLookup);
代替
customerDataContext.Entry(newCustomerLookup).State = EntityState.Added;
但我仍然收到错误消息。这就是我的 CustomerLookup 实体的样子,我不明白为什么 EF 认为 AccountNumber 是 Key 而不是。 (我在想也许主键和键是两个不同的东西?)
public class CustomerLookUp
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(256)]
public string AccountNumber { get; set; }
[StringLength(256)]
public string MobileNumber { get; set; }
[StringLength(256)]
public string EmailAddress { get; set; }
[StringLength(256)]
public string UpdateCode { get; set; }
[StringLength(256)]
public string UmbracoMemberId { get; set; }
public bool UpdateNotification { get; set; }
public DateTime LastUpdated { get; internal set; }
}
这是我的上下文类,只有 2 个数据库集 未 链接。 我查看了数据库以查看生成的内容及其两个表是否符合预期。另一个实体“客户”确实有 AccountNumber 作为 PK,但我没有尝试保存到该表。
public partial class CustomerContext : DbContext
{
public CustomerContext()
: base("name=CustomerContext")
{
}
public DbSet<Customer> Customers { get; set; }
public DbSet<CustomerLookUp> CustomerLookUps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
为了完整起见,这是 Customer 类。 (这个有AccountNumber作为PK,但这是在一个完全不同的表中,我也没有尝试保存。
public class Customer
{
[Key]
[Required]
[StringLength(256)]
public string AccountNumber { get; set; }
[Required]
[StringLength(256)]
public string SupplyPostCode { get; set; }
[StringLength(256)]
public string Title { get; set; }
[StringLength(256)]
public string Forenames { get; set; }
[StringLength(256)]
public string Name { get; set; }
}
更新:我取得了进展,但没有完全解决问题。我决定做的是重命名列,因此我在 CustomerLookUp 类中的 AccountNumber 的每个表中有两个不同的名称,我将名称更改为 AccountNumberX,然后再次运行治疗。我仍然遇到同样的错误。这肯定意味着问题与我试图保存的实体无关。 EF 在抱怨 Customer 类。
在最终调用 CreateCustomerLookUp 方法的业务逻辑中,我正在检索客户记录并对其进行解密,我需要这样做以读取纯文本值,但我并不要求 EF 保存这些更改。
所以发生的情况是,当我保存 newCustomerLookup 对象时,它也在尝试保存我之前检索到的客户记录的解密版本。
所以真正的问题是,我如何告诉 EF 我不希望它保存对该对象所做的更改?我只想保存新的 newCustomerLookup 对象。
【问题讨论】:
-
你能在你的数据库中查看表中的主键定义吗?
-
请发布您的 CustomerDataContext 的代码。
-
您是否将属性设置为上下文类中的键?
-
检查了数据库...客户查表没有得到Account as PK
-
SaveChanges是否抛出异常?在调试模式下,检查Customer类型的 DbContext 上附加的实体是什么,并查看是否有正在修改的属性AccountNumber。Customer和CustomerLookup之间也有FK关系吗?
标签: c# entity-framework entity-framework-6