Page 1 of 1

Code First Migrations - Incorrect Column Nullablity

Posted: Thu 09 Feb 2012 14:21
by ndroe
I have the following entity:

Code: Select all

    public class MyEntity
    {
        public long Id { get; set; }
        public DateTime? DeletedAt { get; set; }
    }
With the following configuration:

Code: Select all

    public class MyEntityConfiguration : EntityTypeConfiguration
    {
        public MyEntityConfiguration()
        {
            HasKey(m => m.Id);
            Property(m => m.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(m => m.DeletedAt)
                .IsOptional();
        }
    }
The following migration is generated:

Code: Select all

    public class MyMigration : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "MyEntities",
                c => new
                {
                    Id = c.Long(nullable: false, identity: true),
                    DeletedAt = c.DateTime(),
                })
                .PrimaryKey(t => t.Id);
        }

        public override void Down()
        {
            DropTable("MyEntities");
        }
    }
Which generated the following SQL

Code: Select all

CREATE TABLE "MyEntities" ( 
  "Id" NUMBER(18) NOT NULL,
  "DeletedAt" TIMESTAMP NOT NULL,
  PRIMARY KEY ("Id")
)
CREATE SEQUENCE "MyEntities_SEQ"
CREATE OR REPLACE TRIGGER "MyEntities_INS_TRG"
  BEFORE INSERT ON "MyEntities"
  FOR EACH ROW
BEGIN
  SELECT "MyEntities_SEQ".NEXTVAL INTO :NEW."Id" FROM DUAL;
END;
Notice that DelatedAt is created as not null when its listed in the configuration as optional and not set as nullable in the migration.

Is this a bug or am I doing something wrong?

Posted: Thu 09 Feb 2012 14:36
by ndroe
After looking it over a little more, it seems like its making all columns not nullable regardless of the "nullable" setting in the migration.

Posted: Tue 14 Feb 2012 13:09
by Shalex
Please set "nullable: true" for the DeletedAt property in your migration:

Code: Select all

   DeletedAt = c.DateTime(nullable: true),
This will generate the column with the NULL option in SQL:

Code: Select all

  "DeletedAt" TIMESTAMP NULL,
Standard (Microsoft's) migration generator is used now. So, we cannot change default nullable setting in the migration.

As we mentioned at our Entity Framework Code-First Migrations support for Oracle, MySQL, PostgreSQL and SQLite article, the implementation of the possibility to generate migrations with powerful and flexible Update To Database Wizard (Entity Developer) is on our roadmap.

Posted: Fri 17 Feb 2012 16:39
by Shalex
The Code-First Migrations behaviour will be changed starting from the next build: if the nullable value of the property in migration is not set explicitly, the corresponding column will be created nullable. We will post here when the corresponding build of dotConnect for Oracle is available for download.

Posted: Mon 27 Feb 2012 14:34
by Shalex
New build of dotConnect for Oracle 6.70.311 is available for download now!
It can be downloaded from http://www.devart.com/dotconnect/oracle/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://www.devart.com/forums/viewtopic.php?t=23469 .