I’m having an odd problem, I can’t get an object from the DB and then reattach it. I have written code like this for other projects but for some odd reason it throws the following error in this application:
System.InvalidOperationException: The EntityKey property can only be set when the current value of the property is null.
here is my code:
Entities.Customer customer;
using (var context = new XXEntities())
{
IQueryable<Entities.Customer> query = from a in context.Customers select a;
query = query.Where(c => c.CODE == "003046");
customer = query.ToList()[0];
}
using (var context = new XXEntities())
{
context.Customers.AddObject(customer);
context.SaveChanges();
}
In the 2nd block,
EntitySet.Add()means add a new object. You want to add an existing object.