简体   繁体   中英

Exception Cannot insert explicit value for identity column in table

Using FluentAPI I am autoincreamenting the primary key of my Table.

FluentAPI configuration are as follows:

mb.ToTable("StationeryItems");
builder.HasKey(c => c.StationeryItemId);

The Exception I get:

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

Try one of the following, maybe they fix your problem. DatabaseGeneratedOption :

[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int StationeryItemId { get; set; }

Or:

builder.Entity<StationeryItems>().Property(t => t.StationeryItemId) 
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

And in EF Core:

builder.Entity<StationeryItems>().Property(b => b.StationeryItemId).ValueGeneratedOnAdd();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM