Lv.的博客

EF Code First数据库映射规则及配置


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 ProductFluent API:

指定表名

1 protected override void OnModelCreating(DbModelBuilder modelBuilder)2 {3     modelBuilder.Entity<Product>().ToTable("Product");4 }指定表名及用户

1 protected override void OnModelCreating(DbModelBuilder modelBuilder)2 {3     modelBuilder.Entity<Product>().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")

posted @ 2016-06-14 23:50  Avatarx  阅读(523)  评论(0编辑  收藏  举报