Migration created that already matches the pattern

I have a class similar to

public class Foo
{
    public virtual string Bar { get; set; }
    // Other Stuff
}

Now we need to consider it Baras a CHAR (8), so I changed the property to

    [StringLength(8)]
    [Column(TypeName = "char")]
    public virtual string Bar { get; set; }

There are many migrations in a project, usually adding new classes or new properties to existing classes. I know that I can just create a new migration that will change the type of the column, but seemed a bit confusing, to first create the column as NVARCHAR (Max) and then change it to CHAR (8). I edited the initial migration, changing

CreateTable(
    "dbo.Foo",
    c => new
        {
            Bar = c.String(),
            // Other stuff
        })

to

CreateTable(
    "dbo.Foo",
    c => new
        {
            Bar = c.String(maxLength: 8, fixedLength: true, storeType: "char", unicode: false),
            // Other stuff
        })

Then I deleted the database and ran a program that used the context, thereby recreating the database.

, . , Foo CHAR (8). , AutomaticMigrationsDisabledException.

, , EF,

public override void Up()
{
    AlterColumn("dbo.Foo", "Bar", c => c.String(maxLength: 8, fixedLength: true, unicode: false));
}

public override void Down()
{
    AlterColumn("dbo.Foo", "Bar", c => c.String());
}

Up() , , ( ), Down() NVARCHAR (Max), .

EF , , , NVARCHAR (Max), CHAR (8) ?

+4

Source: https://habr.com/ru/post/1629080/


All Articles