Update primary key value using entity framework
.net, entity-framework, sql-server, vb.net
Solution
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:
---Customers----
AutoNumber PK <- This links to all other tables in your database, and does NOT change.
CustomerID <- This can change.
CustomerType <- Anonymous or logged in.
And when they log in you change the CustomerType and CustomerID to what you need.
So your query could look like this:
Dim customer As Customer = (From c In db.Customer _
Where c.CustomerID = {Some temp ID} _
AndAlso c. CustomerType = "Anonymous").FirstOrDefault
// After user logs in.
customer.CustomerID = {Make a new user ID here}
customer.CustomerType = "LoggedIn" {or what ever}
db.SaveChanges()
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).
Problem
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!