Skip to content

wrong maxlength when use MigrationBuilder to add column #7749

@hi-xiaoxin

Description

@hi-xiaoxin

hi:

i tried below code in my program ,it's worked . but the column's length is not 200 in database, it's nvarchar(1), am i doing wrong way ?

thank you

xiaoxin

        public static void UpdateDatabase(string connectionString)
        {
            var builder = new DbContextOptionsBuilder<DynamicModelDbContext>();
            builder.UseSqlServer(connectionString);
            var ctx = new DynamicModelDbContext(builder.Options);

            MigrationBuilder builder1 = new MigrationBuilder("SqlServer");
            builder1.AddColumn<string>(
                name: "Description",
                table: "MyTable",
                type: "nvarchar",
                maxLength: 200);

            var generator = ctx.GetService<IMigrationsSqlGenerator>();
            var connection = ctx.GetService<IRelationalConnection>();
            var commands = generator.Generate(builder1.Operations);

            foreach (var cmd in commands)
                cmd.ExecuteNonQuery(connection);
        }

Further technical details

EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win10
IDE: Visual Studio 2015

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions