Pages

Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts

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();


ShareThis