Lücks
Lücks

Reputation: 3994

EntityFramework CodeFirst Ignoring MaxLength

I'm working with SQL Server Compact. For each table I have a Configuration that contains a MaxLength property.

I run Add-Migration and it generates correctly the create column string with maxlength, but when I run Update-Database the column is being created with NVARCHAR(MAX)

public class Test{
    public string TestField { get; set; }
}

public class TestConfiguration : EntityTypeConfiguration<Test>
    {
        public TestConfiguration()
        {

            Property(o => o.TestField)
                .HasMaxLength(100);


        }
    }

My DB Context Class:

public class PlutoContext : DbContext
{
    public PlutoContext()
        : base("name=PlutoContext")
    {
        this.Configuration.LazyLoadingEnabled = false;
        Database.SetInitializer<PlutoContext>(null);
    }

    public virtual DbSet<Test> Tests { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new TestConfiguration());
    }

Generated Migration

public partial class AddTest : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Test",
            c => new
                {
                   Test = c.String(nullable: false, maxLength: 100),
    }

}

Why Framework is not respecting the MaxLength property? Thanks!

Upvotes: 0

Views: 512

Answers (1)

Goksin Gorsun
Goksin Gorsun

Reputation: 1

Please try to add something like the line below in OnModelCreating part of context:

modelBuilder.Configurations.Add(new TestConfiguration());

Upvotes: -1

Related Questions