HasKey() - Primary Key    

 

指定属性为主键 

// Primary Key modelBuilder.Entity<OfficeAssignment>().HasKey(t => t.InstructorID);

也可以指定多个属性为联合主键
// Composite Primary Key
modelBuilder.Entity<Department>().HasKey(t => new { t.DepartmentID, t.Name });


HasDatabaseGeneratedOption()

为数字型主键取消数据库生成
// Switching off Identity for Numeric Primary Keys
modelBuilder.Entity<Department>()
            .Property(t => t.DepartmentID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

HasMaxLength() - Specifying the Length on a Property

指定属性长度
// Specifying the Maximum Length on a Property
modelBuilder.Entity<Department>()
            .Property(t => t.Name)
            .HasMaxLength(50);

IsRequired() - Configuring the Property to be Required

必填字段

// Configuring the Property to be Required

modelBuilder.Entity<Department>()
            .Property(t => t.Name)
            .IsRequired();

Ignore() - Specifying Not to Map a CLR Property to a Column in the Database

忽略
// Specifying Not to Map a CLR Property to a Column in the Database
modelBuilder.Entity<Department>()
            .Ignore(t => t.Budget);

HasColumnName() - Mapping a CLR Property to a Specific Column in the Database

指定列名
// Mapping a CLR Property to a Specific Column in the Database
modelBuilder.Entity<Department>()
            .Property(t => t.Name)
            .HasColumnName("DepartmentName");

HasColumnType() - Configuring the Data Type of a Database Column

指定列类型
// Configuring the Data Type of a Database Column
modelBuilder.Entity<Department>()
            .Property(p => p.Name)
            .HasColumnType("varchar");


ToTable() - Mapping an Entity Type to a Specific Table in the Database

映射表名
// Mapping an Entity Type to a Specific Table in the Database
modelBuilder.Entity<Department>()
            .ToTable("t_Department");


详细参考:http://www.cnblogs.com/panchunting/p/entity-framework-code-first-fluent-api-configuring-mapping-properties-and-types.html

 

posted on 2016-03-01 21:40  xiaotank  阅读(433)  评论(0)    收藏  举报