ralmsdeveloper / EntityFrameworkCore.FirebirdSQL

FirebirdSQL database provider for Entity Framework Core.
Other
44 stars 26 forks source link

Migrations fail on columns with default values #8

Closed robertkruis closed 6 years ago

robertkruis commented 6 years ago

The issue

When creating an entity with a property that should have a default value, e.g. calling HasDefaultValue(), the migration will fail with a "Dynamic SQL error". For me, this has only occurred on columns that weren't an identity column.

Steps to reproduce

  1. Create an entity with properties that should have default values in the DB.

    public class Schedule : Entity<int>
    {
        public Schedule()
        {
        }
    
        // Columns omitted for brevity.        
    
        public ScheduleType ScheduleType { get; set; }
        public ScheduleState ScheduleState { get; set; }
    }
  2. Configure the entity with the Fluent API

    internal class ScheduleMapper : Entity<Schedule>
    {
        public ScheduleMapper(EntityTypeBuilder<Schedule> builder)
        {
            // Properties omitted for brevity
    
            builder.Property(s => s.ScheduleType)
                .HasDefaultValue(ScheduleType.Work); // Should have the default value of the ScheduleType enum.
    
            builder.Property(s => s.ScheduleState)
                .HasDefaultValue(ScheduleState.Valid); // Should have the default value of the ScheduleState enum.
        }
    }
  3. Generate the migration by invoking dotnet ef migrations add InitialMigration

  4. Update the database by invoking dotnet ef database update

Error details

When executing step 4 of the steps to reproduce, an exception like the one below will occur.

Failed executing DbCommand (30ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "Schedule" (
    "Id" INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "CreatedBy" VARCHAR(8191),
    "CreatedDate" TIMESTAMP NOT NULL,
    "CreatorId" INTEGER NOT NULL,
    "Description" VARCHAR(8191),
    "Location" VARCHAR(8191),
    "ModifiedBy" VARCHAR(8191),
    "ModifiedDate" TIMESTAMP,
    "ScheduleState" INTEGER NOT NULL DEFAULT 1,
    "ScheduleType" INTEGER NOT NULL DEFAULT 1,
    "TimeEnd" TIMESTAMP NOT NULL,
    "TimeStart" TIMESTAMP NOT NULL,
    "Title" VARCHAR(8191),
    "Version" VARCHAR(8191),
    CONSTRAINT "PK_Schedule" PRIMARY KEY ("Id"),
    CONSTRAINT "FK_Schedule_User_CreatorId" FOREIGN KEY ("CreatorId") REFERENCES "User" ("Id") ON DELETE NO ACTION
);
...
FirebirdSql.Data.FirebirdClient.FbException (0x80004005): Dynamic SQL Error
SQL error code = -104
Token unknown - line 10, column 38
DEFAULT ---> Dynamic SQL Error
SQL error code = -104
Token unknown - line 10, column 38
DEFAULT
   at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteNonQuery() in C:\Users\Jiri\Documents\devel\NETProvider\working\Provider\src\FirebirdSql.Data.FirebirdClient\FirebirdClient\FbCommand.cs:line 478
   at System.Data.Common.DbCommand.ExecuteNonQueryAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at EntityFrameworkCore.FirebirdSql.Storage.Internal.FirebirdRelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Dynamic SQL Error
SQL error code = -104
Token unknown - line 10, column 38
DEFAULT

Further technical details

Firebird version: Firebird-3.0.2.32703-0_x64 EntityFrameworkCore.Firebird version: 2.0.7

Other details about my project setup: N.A.