I want to create a copy my object in my DB with using Entity Framework
first I got my “Book” from DB
var entity1 = new testEntities();
var book= entity1.Books.First();
entity1.Dispose();
then, I tried to add this object as a new object
var entity2 = new testEntities();
book.Id = 0;
entity2.SaveChanges();
entity2.Dispose();
Also I trid to initilize EntityKey of Book
it didnt work
Is there any way doing this without creating new Book and copy properties from old one?
Thanks
You need to fetch the entity, change the
EntityStatetoAddedin theObjectStateManagerand callSaveChanges:This will copy your ‘book’.