2011-04-15 82 views
24

的長度正如標題暗示:實體框架4.1代碼第一種方法:如何定義屬性

怎麼可能告訴實體框架4.1代碼中第一種方法,我確實需要一些屬性(在特定類型的字符串)有256的長度,或nvarchar(最大),或...

因此,如果這是我的例子模型

public class Book{ 
    public string Title { get; set; } //should be 256 chars 
    public string Description {get;set} //should be nvarchar(max) 
} 

怎麼可能被定義?

在此先感謝!

+0

可能的重複[實體框架驗證混淆 - 最大字符串長度'128'。](http://stackoverflow.com/questions/5414611/entity-framework-validation-confusion-maximum-string-length-of- 128) – 2011-04-15 14:31:53

+0

@Ladislav是的,它幾乎是重複的,但稍有不同!我想出瞭如何:只需使用DataAnnotation,例如[StringLength(1000)]或[MaxLength] – 2011-04-15 14:35:11

回答

43

在EF4.1 RTW中,默認長度爲SQL Server的nvarchar(max)和SQL CE的nvarchar(4000)。要改變長度兼用StringLengthMaxLength註解或流利的映射HasMaxLength

[StringLength(256)] 
public string Title { get; set; } 

或者

[MaxLength(256)] 
public string Title { get; set; } 

或者

modelBuilder.Entity<Book>() 
      .Property(p => p.Title) 
      .HasMaxLength(256); 
3

正如我的意見陳述,這很簡單。
只需使用[StringLength(1000)][MaxLength]DataAnnotation