加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – EntityFramework核心 – 复制实体并将其放回数据库

发布时间:2020-12-15 08:27:13 所属栏目:百科 来源:网络整理
导读:是否有最佳实践来复制实体,根据用户输入对其进行一些更改,然后将其重新插入数据库? 其他一些Stackoverflow线程已经提到过,即使数据库中存在相同的主键,EF也会为您处理插入新对象,但我不太确定EF Core是如何处理它的.每当我尝试复制一个对象时,我都会收到错
是否有最佳实践来复制实体,根据用户输入对其进行一些更改,然后将其重新插入数据库?

其他一些Stackoverflow线程已经提到过,即使数据库中存在相同的主键,EF也会为您处理插入新对象,但我不太确定EF Core是如何处理它的.每当我尝试复制一个对象时,我都会收到错误

Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF

基本上我只需要一种简洁的方法来复制对象,然后将该副本插回到数据库中,并使Id自动递增.有没有最佳实践或简单的方法,而无需手动将属性设置为null或空?

编辑:从数据库中检索对象的示例代码:

public Incident GetIncidentByIdForCloning(int id)
    {
        try
        {
            return _context.Incident.Single(i => i.IncidentId == id);
        }
        catch
        {
            return null;
        }
    }

检索对象后的代码(因为某些字段是自动生成的,如RowVersion,它是一个时间戳):

public IActionResult Clone([FromBody]Incident Incident)
    {
        var incidentToCopy = _incidentService.IncidentRepository.GetIncidentByIdForCloning(Incident.IncidentId);
        incidentToCopy.IncidentTrackingRefId = _incidentService.IncidentRepository.GetNextIdForIncidentCategoryAndType(
            Incident.IncidentCategoryLookupTableId,Incident.IncidentTypeLookupTableId).GetValueOrDefault(0);
        incidentToCopy.RowVersion = null;
        incidentToCopy.IncidentId = 0; //This will fail with or without this line,this was more of a test to see if manually setting would default the insert operation,such as creating a brand new object would normally do.
        incidentToCopy.IncidentCategoryLookupTableId = Incident.IncidentCategoryLookupTableId;
        incidentToCopy.IncidentTypeLookupTableId = Incident.IncidentTypeLookupTableId;
        var newIncident = _incidentService.IncidentRepository.CreateIncident(incidentToCopy);
...

我意识到我可以制作一个全新的对象并进行左手复制,但这看起来非常低效,我想知道EF Core是否提供了更好的解决方案.

解决方法

因此,我在创建这个之前偶然发现了“可能重复”的线程,比我最初偶然发现的那样,并且我忽略了一个不那么高度推崇的解决方案,基本上只是抓住了所有的值.一次从数据库中检索对象时 – 它不会在进程中检索对该对象的引用.我的代码现在看起来像这样:
try
{
    var incidentToCopy = _context.Incident.Single(i => i.IncidentId == id);
    return (Incident) _context.Entry(incidentToCopy).CurrentValues.ToObject();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读