简体   繁体   中英

Erro: Cannot insert explicit value for identity column in table - INT to tinyInt

I'm working on a ASP .NET WEB EF project. I get this error while trying to update the database with 'Update-Migration'.

It says: Error Number:544,State:1,Class:16 Cannot insert explicit value for identity column in table 'Genres' when IDENTITY_INSERT is set to OFF.

Others had this issue before. I tried to apply methods suggested by others on this issue on this website. Among the solutions, one could probably be a fix for my problem is that I have type tinyInt for the PK of the table. That make sense, because i have changed it via [Required] data anotation.

Question: How can I change it back to type INT or simply solve this issue?

Link to the suggestions I followed is : EF Code First giving me error Cannot insert explicit value for identity column in table 'People' when IDENTITY_INSERT is set to OFF.

At the bottom of the thread is mentioned about setting : this.Property(t => t.TableID).HasColumnName("TableID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

But it is not mentionen where? I use VS 2017.

This is the genre class:

 public class Genre
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public int ID { get; set; }

    [Required]
    [StringLength(255)]
    public string Name { get; set; }
}

Here is the blank Migration that I have created and then populated with sql code:

    // <auto-generated />

using System.ComponentModel.DataAnnotations.Schema;

namespace MyNotes.Migrations
{
    using System.CodeDom.Compiler;
    using System.Data.Entity.Migrations.Infrastructure;
    using System.Resources;

    [GeneratedCode("EntityFramework.Migrations", "6.2.0-61023")]
    public sealed partial class PopulateGenresTable : IMigrationMetadata
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

        private readonly ResourceManager Resources = new ResourceManager(typeof(PopulateGenresTable));

    string IMigrationMetadata.Id
    {
        get { return "201809211642586_PopulateGenresTable"; }
    }

    string IMigrationMetadata.Source
    {
        get { return null; }
    }

    string IMigrationMetadata.Target
    {
        get { return Resources.GetString("Target"); }
    }
    }
}

The error appear when I try to run Update-Database after creating changes to this file:

namespace MyNotes.Migrations
{
    using System.Data.Entity.Migrations;

    public partial class PopulateGenresTable : DbMigration
    {
        public override void Up()
        {
            Sql("INSERT INTO Genres (ID, Name) VALUES (1,'Medicine')");
            Sql("INSERT INTO Genres (ID, Name) VALUES (2,'Philosophy')");

        }

        public override void Down()
        {
            Sql("DELETE FROM Genres WHERE ID IN(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)");
        }
    }
}

Well, because you declared the ID as an autogenerated column, SQL Server won't allow you to specifically insert values for this column. So you should add records as follows:

Sql("INSERT INTO Genres (Name) VALUES ('Medicine')");
Sql("INSERT INTO Genres (Name) VALUES ('Philosophy')");

Add this to your model property so it handles auto incrementing for you.

[ScaffoldColumn(false)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

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