2016-03-29 34 views
0

我正在使用SQL Server Compact。对于每个表,我都有一个包含MaxLength属性的配置。EntityFramework CodeFirst忽略MaxLength

我运行添加迁移并正确生成与最大长度的创建列字符串,但是当我运行Update-Database中的列将被NVARCHAR(MAX)创建

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

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

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


     } 
    } 

我的DB上下文类:

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()); 
    } 

生成迁移

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

} 

为什么Framework不尊重MaxLength属性?谢谢!

+0

一切我在这里看到的期待权。唯一不同于我的是您使用的是SQL Compact Edition。也许这是造成这个问题?也许只是为了查看它是否有效而尝试使用常规SQL。 –

+0

SQL Server Compact没有nvarchar(max)数据类型 - 你在哪里看到这个? – ErikEJ

+0

hi @ErikEJ我正在使用LINQPad –

回答

-1

请尽量像线方面的OnModelCreating部分添加的东西如下:

modelBuilder.Configurations.Add(new TestConfiguration()); 
相关问题