JetEntityFrameworkProvider icon indicating copy to clipboard operation
JetEntityFrameworkProvider copied to clipboard

Migration does not mark column as nullable

Open erikvdv1 opened this issue 7 years ago • 2 comments

When changing the type of a column from int to int? the migration below is generated, however when I apply the migration the column is not marked as nullable (i.e. is still marked as required in Access). If I create an initial migration it does create the column with the correct settings.

public partial class MakeNullable : DbMigration
    {
        public override void Up()
        {
            AlterColumn("dbo.Items", "SomeProp", c => c.Int());
        }
        
        public override void Down()
        {
            AlterColumn("dbo.Items", "SomeProp", c => c.Int(nullable: false));
        }
    }

erikvdv1 avatar Sep 02 '17 12:09 erikvdv1

In your code Up is setting the column to nullable, Down to not nullable. After Up the column should be nullable.

I'm running this tests

CreateTable(
        "MigrationMakeNullableInt",
        c => new
        {
            Id = c.Int(nullable: false, identity: true),
            Counter = c.Int(nullable: true)
        })
    .PrimaryKey(t => t.Id);

AlterColumn("dbo.MigrationMakeNullableInt", "Counter", c => c.Int(nullable: false));

These are the DDL statements generated by provider

CREATE TABLE [MigrationMakeNullableInt] (
 [Id] int not null identity(1,1)
, [Counter] int null
);
ALTER TABLE [MigrationMakeNullableInt] ADD CONSTRAINT [PK_MigrationMakeNullableInt_8805946a] PRIMARY KEY ([Id]);
ALTER TABLE [MigrationMakeNullableInt] ALTER COLUMN [Counter] int not null;

At the end the table has the Counter column set to null.

Probably the problem is different. Do you have values in every record?

bubibubi avatar Sep 19 '17 15:09 bubibubi

I've tested it with an empty table and with a table with records. The records have values because the column is non-nullable. The issue remains in both scenarios.

The issue occurs only when migrating an existing database. If the database is created from scratch (even with multiple migrations in a row) all is fine.

erikvdv1 avatar Sep 20 '17 19:09 erikvdv1