Search code examples
c#entity-framework-corefirebirdfirebird-3.0firebird-.net-provider

EF Core - Firebird Migration auto-increment problem


I'm testing the migration with EF Core with MSSQL-Server and Firebird 3.0.

I create a new table with a few fields. The key-field has the property .ValueGeneratedOnAdd(), but the key-field in the Firebird database doesn't get an auto-increment during the migration. It works with the MS-SQL-Server correctly.

I use the framework FirebirdSql.EntityFrameworkCore.Firebird version 6.6.0.

modelBuilder.Entity("GenerateCodeFromDB.DB_Modell.TblTest", b =>
{
    b.Property<long>("Id")
        .ValueGeneratedOnAdd()
        .HasColumnName("ID")
        .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

    b.Property<DateTime?>("Date");

    b.Property<string>("Name")
        .HasMaxLength(50);

    b.Property<int?>("Number");

    b.HasKey("Id");

    b.ToTable("tblTest");
});

Solution

  • I got the solution on http://tracker.firebirdsql.org/browse/DNET-884. I have to add .ForFirebirdUseIdentityColumns() in OnModelCreating in the DBContext class. Now I get the auto-increment in both database-types.

    It looks now like this:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("ProductVersion", "2.2.4-servicing-10062")
                .ForFirebirdUseIdentityColumns()
                .ForSqlServerUseIdentityColumns();
    
            modelBuilder.Entity<TblTest>(entity =>
            {
                entity.HasKey(e => e.Id)
                    .HasName("PK__tblTest");
    
                entity.Property(e => e.Name).IsUnicode(false);
            });
        }
    

    After adding the migration I have to an annotation in the createTable-parameter for the second database-type I'm using. In my case I added the migration with Firebird and added the annotation for the SqlServer:

            migrationBuilder.CreateTable(
                name: "tblTest",
                columns: table => new
                {
                    ID = table.Column<long>(nullable: false)
                        .Annotation("Fb:ValueGenerationStrategy", FbValueGenerationStrategy.IdentityColumn)       
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Number = table.Column<int>(nullable: true),
                    Name = table.Column<string>(unicode: false, maxLength: 50, nullable: true),
                    Date = table.Column<DateTime>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK__tblTest", x => x.ID);
                });