Insert a copy of existing row using Linq to Entities [closed]

冷暖自知 提交于 2019-12-20 02:28:19

问题


I am using entity framework. I have a table with auto generated primary key and lot of foriegn key relationship with other master data tables. We need a copy functionality, wherein we can select any existing row from the table and insert that as a copy in the same table.

I need to copy all the fields, but it should be a different record, and need the procedure/LINQ for same.

Please suggest, how should I proceed.


回答1:


You can do this:

context.Entry(oCustomer).State = EntityState.Added;

this will cause EF to think this entity is a new one and after you call SaveChanges it will be inserted in the database and not updated.

If you have an object graph - you need to traverse it and make sure all child entities are set to EntityState.Unchanged or they will be duplicated after SaveChanges.



来源:https://stackoverflow.com/questions/20322778/insert-a-copy-of-existing-row-using-linq-to-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!