Entity Framework Code First 系列 3——约定及配置
<div id="cnblogs_post_body"> 在上一节中我们讲解了一个简单的entity framework application。在这一节里我们将使用Data annotation和 Fluent API 进行 实体模型的配置。这里我们主要讲解的是通过Fluent API 来进行配置,因为相对于Data Annotation 我更喜欢这种干净的代码而且实体代码是自动生成的使用Fluent API的方式更加灵活和方便。1. Length &mdash;&mdash;主要描述数据的长度
Data Annotation
MinLength(nn)
MaxLength(nn)
StringLength(nn)
FluentAPI
Entity<T>.Property(t=>t.PropertyName).HasMaxLength(nn)
继续上一节中示例,对于SchoolName 或 StudentName 之内的字段我们要对它的长度进行约定此时我们就可以通过配置来完成。
采用Data Annotation 的方式,在之后的例子中我将不再采用此种方式。
<div class="cnblogs_code" >http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gifhttp://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gifDataAnnotation<div id="cnblogs_code_open_6c4a54ee-9bbd-448f-a250-b96534de3c10" class="cnblogs_code_hide">100)] public string SchoolName { get; set; }
页:
[1]