I’m trying to update one value of a compound primary key from within the entity framework and I’m getting this error: “The property ‘CustomerID’ is part of the object’s key information and cannot be modified. “
Here is my code:
Dim customer As Customer = (From c In db.Customer Where c.CustomerID = "xxx" AndAlso c.SiteKey = siteKey).FirstOrDefault
customer.CustomerID = "fasdfasdf"
db.SaveChanges()
It seems too simple. Is it true you can’t update a primary key within the entity framework? I can’t find any documentation on the topic. Thanks!
You can’t and for good reason. See KM comments.
One thing I say you could do is have two tables one with anonymous data and one that stores the the real user data after they log in.
Or your could (not tested or ever done by me) is have this kind of table layout:
And when they log in you change the CustomerType and CustomerID to what you need.
So your query could look like this:
Note that the autonumber primary key never changes. This is so that any tables that you have in a relationship with the Customers table still work and don’t have to do cascading updates on the primary key (which is like stabbing yourself in the eye with a pencil).