Pages

Saturday, February 4, 2017

using Entity Framework or EF to copy an Object.

using Entity Framework or EF to copy an Object.
This can be used to avoid Error : PK cannot be modified :
"The property 'MyPrimaryKey' is part of the object's key information and cannot be modified. "
we can use this query :
var item = _context.MyTableName.First(z => z.Name == "Ardi");
var newItem = (MyTableName)_context.Entry(item).GetDatabaseValues().ToObject();
newItem.MyPrimaryKey = "Generated_" + DateTime.Now.ToString();
_context.SaveChanges();


No comments:

Post a Comment

ShareThis