Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect migration for changing the key from manual ID to IDENTITY #6285

Closed
mafshin opened this issue Aug 10, 2016 · 1 comment
Closed

Incorrect migration for changing the key from manual ID to IDENTITY #6285

mafshin opened this issue Aug 10, 2016 · 1 comment

Comments

@mafshin
Copy link

mafshin commented Aug 10, 2016

Steps to reproduce

Having this model

    public class Book
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class BookContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Data Source=.\\SQLExpress;Initial Catalog=Books;Integrated Security=True");

            base.OnConfiguring(optionsBuilder);
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Book>().Property(x => x.ID).ValueGeneratedNever();            

            base.OnModelCreating(modelBuilder);
        }

        public DbSet<Book> Books { get; set; }
    }

in which Book.ID must be set manually:

  • Add-Migration Initial
  • Update-Database
  • Make the ID property to be IDENTITY by commenting ValueGeneratedNever() line
            //modelBuilder.Entity<Book>().Property(x => x.ID).ValueGeneratedNever();   
  • Add-Migration Set_Book_ID_As_Identity
  • Update-Database

The issue

The generated migration for setting the ID to IDENTITY is as follows:

  public partial class Set_Book_ID_As_Identity : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<int>(
                name: "ID",
                table: "Books",
                nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<int>(
                name: "ID",
                table: "Books",
                nullable: false);
        }
    }

resulting in this SQL

ALTER TABLE [Books] ALTER COLUMN [ID] int NOT NULL;

which doesn't set the ID to auto-increment identity.

Expected behavior

To generate a migration like this

            migrationBuilder.DropColumn(
                 name: "ID",
                 table: "Books");

             migrationBuilder.AddColumn<int>(
                name: "ID",
                table: "Books",
                nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

Further technical details

EF Core version: 1.0
Operating system: Windows 10 x64
Visual Studio version: VS 2015 Update 3

@bricelam
Copy link
Contributor

Dupe of #2100 (tracked as part of #329)

@divega divega closed this as completed Aug 10, 2016
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants