Entity|Entity Framework管理一对二实体关系

在上一篇文章中,简单的介绍了使用Fluent API如何管理一对一的实体关系,在这篇文章中,接着介绍Fluent API如何管理一对多的实体关系。
要在数据库中配置一对多关系,我们可以依赖EF约定,还可以使用数据注解或Fluent API来显式创建关系。接下来使用捐赠者Donator和支付方法PayWay这两个类来举例子,这里的一对多关系是:一个人可以通过多种支付方式赞助我。
Entity|Entity Framework管理一对二实体关系
文章图片

支付方式类PayWay结构如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OneToMany.Model.Model{public class PayWay{public int PayWayId { get; set; }public string Name { get; set; }public virtual Donator Donator { get; set; }}}

因为一个赞助者可以通过多种支付方式赞助我,这句话就表明了Donator对象应该有一个PayWay的集合,因此,我们要给Donator类新加入一个集合属性,捐赠者类Donator结构如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OneToMany.Model.Model{public class Donator{public int Id { get; set; }public string Name { get; set; }public string Amount { get; set; }public DateTime DonateDate { get; set; }/// /// PayWay类型的集合属性/// public virtual ICollection PayWays { get; set; }}}

Donator类的配置伙伴类的定义如下:
using OneToMany.Model.Model; using System; using System.Collections.Generic; using System.Data.Entity.ModelConfiguration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OneToMany.Map.Map{public class DonatorMap :EntityTypeConfiguration{public DonatorMap(){ToTable("Donator"); //将Name设置为必须this.Property(p => p.Name).IsRequired(); }}}

PayWay的配置伙伴类的定义如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OneToMany.Model.Model; using System.Data.Entity.ModelConfiguration; namespace OneToMany.Map.Map{public class PayWayMap : EntityTypeConfiguration{public PayWayMap(){ToTable("PayWay"); this.Property(p => p.Name).HasMaxLength(16); }}}

EFDbContext类定义如下:
using OneToMany.Model.Model; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OneToMany.Map.EFContext{public class EFDbContext:DbContext{public EFDbContext(): base("name=CodeFirstApplication"){ }public DbSet PayWays { get; set; }public DbSet Donators { get; set; }protected override void OnModelCreating(DbModelBuilder modelBuilder){// 设置主键modelBuilder.Entity().HasKey(p => p.PayWayId); modelBuilder.Entity().HasKey(p => p.DonatorId); // 设置一对多modelBuilder.Entity().HasMany(p => p.PayWays).WithRequired(t => t.Donator); base.OnModelCreating(modelBuilder); }}}

控制台程序定义如下:
using OneToMany.Map.EFContext; using OneToMany.Model.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OneToManyApplication{class Program{static void Main(string[] args){using (var context = new EFDbContext()){var donator = new Donator{Amount = 6,Name = "虾米",DonateDate = DateTime.Now,PayWays = new List {new PayWay{Name="支付宝"},new PayWay{Name="微信"}}}; context.Donators.Add(donator); context.SaveChanges(); }Console.WriteLine("执行成功"); Console.ReadKey(); }}}

程序运行后数据库结构如下:
Entity|Entity Framework管理一对二实体关系
文章图片

查询数据:
Entity|Entity Framework管理一对二实体关系
文章图片

【Entity|Entity Framework管理一对二实体关系】到此这篇关于Entity Framework管理一对二实体关系的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读