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

Int32/Int64 properties with default value automatically annotated with SqlServerValueGenerationStrategy.IdentityColumn in migration #5384

Closed
sergeitemkin opened this issue May 17, 2016 · 3 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@sergeitemkin
Copy link

sergeitemkin commented May 17, 2016

After migrating to EF7 RC2 we've noticed that our int and long properties that are set with HasDefaultValue in the ModelBuilder are causing the migration script to annotate columns representing those properties with an identity attribute. This, in turn, causes an exception since defaults cannot be created on identity columns.

Here's a simplified example of the DbContext:

    public class Entity
    {
        public Guid Id { get; set; }
        public int Column1 { get; set; }
    }

    public class TestContext : DbContext
    {
        public DbSet<Entity> Entitys { get; set; }

        public TestContext() { }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var serverName = @"localhost";
            var dbName = "Test";

            optionsBuilder
                .UseSqlServer($"data source={serverName};initial catalog={dbName};Integrated Security=true;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Entity>(entity =>
            {
                entity
                    .Property(e => e.Column1)
                    .HasDefaultValue(1);
            });
        }
    }

    static void Main(string[] args)
    {
        using (var context = new TestContext())
        {
            context.Database.EnsureCreated();
        }
    }

When creating a migration with Add-Migration, I get the following, which causes an exception when context.Database.EnsureCreated is called (Defaults cannot be created on columns with an IDENTITY attribute. Table 'Entitys', column 'Column1'):

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Entitys",
            columns: table => new
            {
                Id = table.Column<Guid>(nullable: false),
                Column1 = table.Column<int>(nullable: false, defaultValue: 1)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Entitys", x => x.Id);
            });
    }

Is there something I'm missing, or is there a workaround for this?

@sergeitemkin sergeitemkin changed the title Int32/Int64 properties with default value annotated with SqlServerValueGenerationStrategy.IdentityColumn Int32/Int64 properties with default value automatically annotated with SqlServerValueGenerationStrategy.IdentityColumn in migration May 17, 2016
@rowanmiller
Copy link
Contributor

"Identity pattern" is a more general concept in EF that means a value is generated for the property for new entities. For example, your Guid Id property is also setup as Identity in the model, since the values are generated for new entities. This is also true for default values, we need to get the values that was generated back from the database when the row is inserted (more obvious when the default is something like GETDATE() rather than a constant of 1).

Looks like an issue in migrations, where the presence of a default value means we shouldn't be trying to also use SQL Server IDENTITY to also handle value generation.

@julianpaulozzi
Copy link

+1

@rowanmiller
Copy link
Contributor

As a workaround for this issue, you should remove the .Annotation(...) call in the scaffolded migration. Obviously that's just a workaround and we'll get this issue fixed for RTM.

AndriySvyryd added a commit that referenced this issue May 24, 2016
…d IdentityColumn configurations

Rename ComputedValueSql extension method to ComputedColumnSql to be consistent with fluent API

Fixes #5384
AndriySvyryd added a commit that referenced this issue May 24, 2016
…d IdentityColumn configurations

Rename ComputedValueSql extension method to ComputedColumnSql to be consistent with fluent API

Fixes #5384
@AndriySvyryd AndriySvyryd removed their assignment Jun 6, 2018
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants