且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EntityFramework Core-复制实体并将其放回数据库

更新时间:2023-02-14 19:27:07

因此,我经历可能重复"线程的次数比我在创建此线程时最初偶然发现它时所经历的要多一些,如此高调的解决方案使我忽略了,当从数据库中检索对象时,它基本上只是立即获取所有值-并且在处理过程中不会检索对该对象的引用.我的代码现在看起来像这样:

So I went through the "Possible duplicate" thread a bit more than I did when I initially stumbled upon it before creating this one, and there was a not-so-highly upvoted solution that I overlooked that essentially just grabs all of the values at once when retrieving the object from the database - and it doesn't retrieve a reference to that object in the process. My code now looks something like this:

try
{
    var incidentToCopy = _context.Incident.Single(i => i.IncidentId == id);
    return (Incident) _context.Entry(incidentToCopy).CurrentValues.ToObject();
}