Entity Framework 4.1 code first approach: how to define length of properties

后端 未结 3 1430
春和景丽
春和景丽 2021-02-07 00:14

As the title implies:

how is it possible to tell Entity Framework 4.1 in code first approach, that i do want some properties (in particular of type string) to have a le

相关标签:
3条回答
  • 2021-02-07 00:24

    As is stated in my comment, it's simple.
    Just use [StringLength(1000)] or [MaxLength] from DataAnnotation.

    0 讨论(0)
  • 2021-02-07 00:25

    You can use the following way If you want a Maximum string length

    [StringLength(Int32.MaxValue)]
    

    Or

    [MaxLength]
    

    Or

    Property(m => m.Title ).IsRequired().HasMaxLength(128);
    

    Or

    [MaxLength(256)]
    
    0 讨论(0)
  • 2021-02-07 00:44

    In EF4.1 RTW default length is nvarchar(max) for SQL Server and nvarchar(4000) for SQL CE. To change the length use either StringLength or MaxLength annotations or fluent mapping HasMaxLength:

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

    Or

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

    Or

    modelBuilder.Entity<Book>()
                .Property(p => p.Title)
                .HasMaxLength(256);
    
    0 讨论(0)
提交回复
热议问题