简体   繁体   English

当IDENTITY_INSERT设置为OFF时,无法在TABLE中为Identity列插入显式值; .NET Core 2.1

[英]Cannot insert explicit value for identity column in TABLE when IDENTITY_INSERT is set to OFF; .NET Core 2.1

I'm trying to clone entities in EF Core. 我正在尝试在EF Core中克隆实体。 As far as I understand, I need to retrieve them AsNoTracking and then re-add them however when I do I get the error: 据我了解,我需要检索它们AsNoTracking,然后重新添加它们,但是当我收到错误消息时:

Cannot insert explicit value for identity column in Exercises when IDENTITY_INSERT is set to OFF; 当IDENTITY_INSERT设置为OFF时,无法在“练习”中为身份列插入显式值;

I added the data annotation: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] to the Id but I'm still getting the same error. 我向ID添加了数据批注: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] ,但仍然遇到相同的错误。

Here is my code: 这是我的代码:

public class Exercise : Entity
{
    public Exercise()
    {
        Sets = new HashSet<Set>();
    }       

    public DateTime Date { get; set; }

    public int MuscleGroupId {get;set;}

    public int MesoId { get; set; }

    public ICollection<Set> Sets { get; set; }

    public void AddSet(Set set)
    {
        Sets.Add(set);
    }
}

And here is my code for Entity which has I set an annotation telling EF Core the ID is database generated: 这是我的Entity代码,我设置了一个注释来告诉EF Core ID是数据库生成的:

 public class Entity
 {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
 }

Here is the code to clone the entities: 这是克隆实体的代码:

 var exercises = _context.Exercises.AsNoTracking().Include(y => y.Sets)
.Where(x => x.MesoId == mesoId && x.Date == fromDate);

 foreach (var exercise in exercises)
   {
      exercise.Date = toDate;
      _context.Exercises.Add(exercise);
   }

 await _context.SaveChangesAsync();

You cannot store values into an Identity column in the database because that value is managed by the database itself. 您不能将值存储到数据库的“标识”列中,因为该值由数据库本身管理。

When "cloning" your Exercise records you need to set Id=0 so that when the new row gets inserted the database will create the Identity value for it, eg: 当“克隆”您的练习记录时,您需要设置Id=0以便在插入新行时数据库将为其创建Identity值,例如:

foreach (var exercise in exercises)
{
   exercise.Date = toDate;
   exercise.Id = 0; //<<--This is important!
   _context.Exercises.Add(exercise);
}
await _context.SaveChangesAsync();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SqlException:当 IDENTITY_INSERT 设置为 OFF ASP.NET Core 2.1 时,无法在表“类别”中插入标识列的显式值 - SqlException: Cannot insert explicit value for identity column in table 'Categories' when IDENTITY_INSERT is set to OFF ASP.NET Core 2.1 当 IDENTITY_INSERT 设置为 OFF 时,无法为标识列插入显式值。 (实体框架核心) - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core) 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表&#39;[table]&#39;中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表&#39;Table&#39;中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为表&#39;ActivityInstances中的标识列插入显式值 - Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;Recipe&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Recipe' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在表&#39;MyTableName&#39;中为标识列插入显式值 - Cannot insert explicit value for identity column in table 'MyTableName' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;AspNetUsers&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在表&#39;candidatedetails&#39;中为identity列插入显式值 - Cannot insert explicit value for identity column in table 'candidatedetails' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM