EF Code First数据库映射规则主要包括以下方面:
1、表名及所有者映射
Data Annotation:
指定表名
1 using System.ComponentModel.DataAnnotations; 2 3 [Table("Product")] 4 public class Product
指定表名及用户
using System.ComponentModel.DataAnnotations; [Table("Product", Schema = "dbo")] public class Product
Fluent API:
指定表名
1 protected override void OnModelCreating(DbModelBuilder modelBuilder) 2 { 3 modelBuilder.Entity().ToTable("Product"); 4 }
指定表名及用户
1 protected override void OnModelCreating(DbModelBuilder modelBuilder) 2 { 3 modelBuilder.Entity().ToTable("Product", "dbo"); 4 }
2、列名映射
Data Annotation:
1 [Column("CategoryID")] 2 public int CategoryID { get; set; } 3 [Required, Column("CategoryName")] 4 public string CategoryName { get; set; }
Fluent API:
1 Property(t => t.CategoryID).HasColumnName("CategoryID"); 2 Property(t => t.CategoryName).IsRequired().HasColumnName("CategoryName")