Entity Framework with MySQL 学习笔记一(关系)

时间:2023-03-08 17:39:37

这一篇说说 EF Fluent API 和 DataAnnotations

参考 :

http://msdn.microsoft.com/en-us/data/jj591617.aspx

http://msdn.microsoft.com/en-us/data/jj591620.aspx

Fluent API 和 DataAnnotations 主要作用是让我们定义数据库映射的关系还有一些比如名字不相等的情况等等。

关系数据库一般我们有 1对1,1对多,多对多的表关系

下面我们来一一列出

注 : 这里的思想是先有表,才有类,这里只是例子,真真开发的时候通常是建类图才建数据库的。

我给一个电子商务的案子

我们有个产品种类的表 (prod_category) 还有一个产品表 (prod) , 1个产品可以在多个种类中出现,1个种类也可以有多个产品,所以他们的关系是 (多对多)

所以呢,我们得要多一个表来维持它们的关系,就叫 prod_category_vs_prod 表吧.

prod_category表

[Table("prod_category")] //使用标签,我们可以修改表名
public class ProdCategory
{
[Key] //标签Key,代表id 是主键
public Int32 id { get; set; }
public string category { get; set; } //种类名字 public virtual ICollection<Prod> prods { get; set; } //一个种类有多个产品,所以这里是个 collection , 记得是virtual哦,原理以后会解释.
}

prod表

[Table("prod")]
public class Prod
{
[Key]
public Int32 id { get; set; }
public string code { get; set; }
public string name { get; set; }
public virtual ICollection<ProdCategory> categorys { get; set; } //这里也可以反向获取到种类集合,一个产品有很多种类
}

好,那我们来定义关系

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Prod>()
.HasMany(prod => prod.categorys) //prod 有很多 categorys
.WithMany(category => category.prods) //category 也有很多prods
.Map(m =>
{
m.ToTable("prod_category_vs_prod");
m.MapLeftKey("prod_category_id");
m.MapRightKey("prod_id");
});
}

.Map 记入了维护它们之间的关系表,也就是 prod_category_vs_prod 建对建

好,接着是 1对0,或1对1 关系

这里想不出特别好的例子,我们假设一个产品有一个或没有细节

细节被记入在一个 prod_detail 表中

[Table("prod_detail")]
public class ProdDetail
{
[Key]
public Int32 id { get; set; }
public string fullDescription { get; set; }
[Column("prod_id")] //用标签需改column名
public Int32 prodId { get; set; } //这个是外键 public virtual Prod prod { get; set; } //一个detail肯定有一个prod
}
    [Table("prod")]
public class Prod
{
[Key]
public Int32 id { get; set; }
public string code { get; set; }
public string name { get; set; } public virtual ProdDetail detail { get; set; } //这里有一个detail,但有可能是null
public virtual ICollection<ProdCategory> categorys { get; set; }
}
1-0
modelBuilder.Entity<ProdDetail>()
.HasKey(detail => detail.prodId); //一对一,所以prodId也算是主键
modelBuilder.Entity<ProdDetail>()
.HasRequired(detail => detail.prod) //detail 一定有 prod
.WithOptional(prod => prod.detail); //prod 不一定有 detail //1 -1
modelBuilder.Entity<ProdDetail>()
.HasKey(detail => detail.prodId); //也算主键
modelBuilder.Entity<Prod>()
.HasRequired(prod => prod.detail) //prod 一定有 detail
.WithRequiredPrincipal(detail => detail.prod); detail 也一定有 prod

最后是1对多关系

比如1个产品有很多颜色

就一个 prod_color表吧

    [Table("prod_color")]
public class ProdColor
{
[Key]
public Int32 id { get; set; }
public string color { get; set; }
[Column("prod_id")]
public Int32 prodId { get; set; } public virtual Prod prod { get; set; } //一定有prod
}

直接上关系代码

//1-n
modelBuilder.Entity<ProdColor>()
.HasRequired<Prod>(color => color.prod) //color 一定有 prod
.WithMany(prod => prod.colors) //prod 有很多 colors 
.HasForeignKey(color => color.prodId); //color 有一个外键 prodId

modelBuilder.Entity<Prod>()
.HasMany<ProdColor>(prod => prod.colors) //prod 有很多 colors 
.WithRequired(color => color.prod) //color 一定有 prod 
.HasForeignKey(color => color.prodId); //color 有外键 prodId

好啦,大致上是这样先。

其实 1-0|1, 1-n 都可以直接用 DataAnnotations 来完成,并不需要用 Fluent API .

只是多对多的关系好像不行,所以呢

其实2个都应该学会,我个人是觉得如果只用标签来描述全部东西也是有点乱,

所以呢,关系就用 Fluent API , 如果是表名字,类型,maxlength, primary key 等等可以用标签来完成。这样分担工作就不乱了咯!

这里给出常用的整理版本

1-1 关系

//DataAnnotation 1-1 table
//SQLtable : member , columns : memberId, name
[Table("member")]
public class Member
{
[Key]
public Int32 memberId { get; set; }
public string name { get; set; }
[Required] //1-1关系最好是放啦
public virtual Address address { get; set; }
}
//SQLtable : address , columns : memberId, postcode, country
[Table("address")]
public class Address
{
[Key, ForeignKey("member")] //关键就是这个关系连接, "member" 是 tableName
public Int32 memberId { get; set; } //这里写addressId 也行
public string postcode { get; set; }
public string country { get; set; }
public virtual Member member { get; set; }
}