I'm using code first Entity Framework (code-first) in my application.
When I try to add a NEW object and save it to the database, EF is throwing this error.
The property 'AccountNumber' is part of the object's key information and cannot be modified.
First thing that is confusing about this error is AccountNumber is not a primary key in that table.
After reading a few posts to solve this, I try to create a brand new object just before it saves it like this:
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();
}
I have also tried
customerDataContext.CustomerLookUps.Add(newCustomerLookup);
Instead of
customerDataContext.Entry(newCustomerLookup).State = EntityState.Added;
But I'm still getting the error. This is what my CustomerLookup entity looks like, I can't figure out why EF thinks AccountNumber is a Key its not. (I was thinking maybe primary key and key are two different things?)
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; }
}
This is my context class only 2 DB-sets THAT ARE NOT linked. I have looked in the DB to see what gets generated and its two tables as expected. The other Entity "Customer" does have AccountNumber as the PK but I am not trying to save to that table.
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)
{
}
}
For completeness this is the Customer class. (This has AccountNumber as PK, but this is in a completely different table, and not one I am trying to save in too.
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; }
}
UPDATE: I have made progress, but not totally solved the problem. What I decided to do was Rename the columns so I had two different names in each table for AccountNumber in the CustomerLookUp class I changed the name to AccountNumberX then run the cure again. I still got the same error. Which means for sure the problem was nothing to do with the entity I was trying to save. EF was complaining about the Customer Class.
Within the business logic that ultimately calls the CreateCustomerLookUp method I am retrieving a customer record and Decrypting it, I need to do this to read the plain text values, but I am not asking EF to save these changes.
So what is happening is when I am saving the newCustomerLookup object it is also trying to save the Decrypted version of the customer record that I retrieved earlier.
So the real question is, how do I tell EF I don't want it to save the changes made to that object? I only want to save the new newCustomerLookup object.
SaveChanges
? In debug mode check to see what the attached entities are on the DbContext of typeCustomer
and see if any have a propertyAccountNumber
that is being modified. Also is there a FK relationship betweenCustomer
andCustomerLookup
?