在实体框架核心中复制整个行 [英] Replicate Entire Row In Entity Framework Core

查看:88
本文介绍了在实体框架核心中复制整个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数据库中检索一行,更改其中的某些列值,并将其添加为新行(Entity Framework Core),

I am trying to retrieve a row from database , changing certain columns value in it and adding it as new row (Entity Framework Core),

但这给了我错误

当IDENTITY_INSERT设置为OFF时,无法为表"Audit_Schedules"中的标识列插入显式值.

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

此表具有主键"ScheduleId"

下面是我的代码

 AuditSchedules _schedules = new AuditSchedules();
 using (var ctx = new QuestionnaireEntities(_configuration))
                {
                    _schedules = ctx.AuditSchedules.Where(x => x.ScheduleId == model.ScheduleID).SingleOrDefault();
                    _schedules.StaffId = model.TransferedAuditorCode;
                    _schedules.StaffName = model.TransferedAuditorName;
                    _schedules.FromDate = _schedules.ToDate = Convert.ToDateTime(model.TransferedScheduleDate);
                    ctx.AuditSchedules.Add(_schedules);
                    ctx.SaveChanges();

                    _subschedules = ctx.AuditSubSchedule.Where(x => x.SubScheduleId == model.SubScheduleID).SingleOrDefault();
                    _subschedules.IsHoliDay = "Y";
                    _subschedules.HolidayType = model.HolidayType;
                    _subschedules.TransferedScheduleId = _schedules.ScheduleId.ToString();
                    ctx.AuditSubSchedule.Update(_subschedules);
                    ctx.SaveChanges();
                }

出现错误

ctx.AuditSchedules.Add(_schedules);

首先,我认为它的Schedule_ID值存在冲突,并且无法添加重复的主键,但是Schedule_ID是自动生成的字段,因此不应出现此问题

First I thought its conflicting in value of Schedule_ID and not able to add duplicate Primary Key , But Schedule_ID is auto generated field so this issue should not occur

我还尝试将其设置为其他值

I also tried setting it to different value

_schedules.ScheduleId = 0;

,但不插入. 如何复制行中几乎没有变化的行(想添加新行但修改值)

but it does not insert . How Can I replicate a row with few changes in it (want to add a new row but modified values)

推荐答案

EF插入时自动生成值的核心行为与EF6不同.

EF Core behavior with auto generated values on insert is different than EF6.

首先,该属性必须具有默认值(0)才能自动生成.这样就可以插入EF6中无法插入的标识.

First, the property must have default value (0) in order to be auto generated. This allows identity inserts which was not possible in EF6.

第二,上下文不应已跟踪添加的实体,因为否则上下文将在内部保留一些有关已设置实体密钥的信息,并将在生成的INSERT中包括值(甚至是0).命令,这反过来会导致您遇到异常.

Second, the entity being added should not be already tracked by the context, because otherwise the context keeps internally some information that the entity key has been set and will include the value (even 0) in the generated INSERT command, which in turn causes the exception you are getting.

要实现该目标,请在之前调用Add方法:

To achieve the goal, before calling the Add method:

首先使用获取无追踪查询

_schedules = ctx.AuditSchedules
    .AsNoTracking() // <--
    .Where(x => x.ScheduleId == model.ScheduleID)
    .SingleOrDefault();

或明确分离

ctx.Entry(_schedules).State = EntityState.Detached;

然后重置PK

_schedules.ScheduleId = 0;

进行其他修改并最终调用

The do other modifications and finally call

ctx.AuditSchedules.Add(_schedules);

这将适用于不带导航属性/FK的简单实体.对于复杂的实体图,您应该不使用跟踪查询,然后以与分离的实体图相同的方式使用它.

This will work for simple entities w/o navigation properties / FKs. For complex entity graph you should use no tracking query, and then work with it the same way as with detached entity graphs.

这篇关于在实体框架核心中复制整个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆