I create a new object and want to attach it to a context like this,
User user = new User();
user.userName=”Kobe”;
context.Attach(user);
An error message appears – “An object with a null EntityKey value cannot be attached to an object context”. If I query out a user object from database and assign it’s EntityKey to the new object, then detach the query result object like this,
User user = (from u in context.Users where u.userID == 1 select u).First();
User newUser = new User();
newUser.userName = “Kobe”;
newUser.EntityKey = user.EntityKey;
context.Detach(user);
context.Attach(newUser);
Another error message appears – “The object cannot be attached because the value of a property that is a part of the EntityKey does not match the corresponding value in the EntityKey.” I really don’t know what the EntityKey is, I searched on the internet and have seen the EntityKey Class in MSDN, but still can’t understand clearly. When the EntityKey created and attached to an object? where can I find it? If I detach the object, why the EntityKey is still exist?
Anyone can help? Thanks in advance!