我在我的應用程序中使用代碼第一個實體框架(代碼優先)。屬性'AccountNumber'是對象的關鍵信息的一部分,不能修改
當我嘗試添加一個NEW對象並將其保存到數據庫時,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 DB-套未鏈接。 我已經在數據庫中查看了預期生成的內容和兩個表。另一個實體「客戶」確實具有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類。 (此具有賬戶號碼作爲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然後再次運行cure我仍然得到相同的錯誤。問題與我試圖挽救的實體無關,EF正在抱怨客戶類別。他最終調用CreateCustomerLookUp方法的業務邏輯我正在檢索客戶記錄並解密它,我需要這樣做來讀取純文本值,但我不要求EF保存這些更改。
因此,當我保存newCustomerLookup對象時,它正在嘗試保存先前檢索到的客戶記錄的Decrypted版本。
所以真正的問題是,我如何告知EF我不希望它保存對該對象所做的更改?我只想保存新的newCustomerLookup對象。
您可以檢查主鍵定義在表中您的數據庫? – Sparrow
請發佈您的CustomerDataContext的代碼。 –
您是否將屬性設置爲上下文類中的鍵? – DavidG