Entity Framework 4.1代码第一种方法:如何定义属性的长度

| 如标题所示: 如何以代码优先的方式告诉Entity Framework 4.1,我确实希望某些属性(尤其是字符串类型)的长度为256或nvarchar(max)或... 所以如果这是我的模型
public class Book{
   public string Title { get; set; } //should be 256 chars
   public string Description {get;set} //should be nvarchar(max)
}
怎么定义呢? 提前致谢!     
已邀请:
在EF4.1 RTW中,默认长度对于SQL Server是nvarchar(max),对于SQL CE是nvarchar(4000)。要更改长度,请使用
StringLength
MaxLength
注释或流畅的映射
HasMaxLength
[StringLength(256)]
public string Title { get; set; }
要么
[MaxLength(256)]
public string Title { get; set; }
要么
modelBuilder.Entity<Book>()
            .Property(p => p.Title)
            .HasMaxLength(256);
    
如我的评论所述,这很简单。 只需使用
DataAnnotation
中的
[StringLength(1000)]
[MaxLength]
。     

要回复问题请先登录注册