在上一篇文章中,简单的介绍了使用Fluent API如何管理一对一的实体关系,在这篇文章中,接着介绍Fluent API如何管理一对多的实体关系。
要在数据库中配置一对多关系,我们可以依赖EF约定,还可以使用数据注解或Fluent API来显式创建关系。接下来使用捐赠者Donator和支付方法PayWay这两个类来举例子,这里的一对多关系是:一个人可以通过多种支付方式赞助我。
支付方式类PayWay结构如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace OneToMany.Model.Model 8 { 9 public class PayWay 10 { 11 public int PayWayId { get; set; } 12 13 public string Name { get; set; } 14 15 public virtual Donator Donator { get; set; } 16 } 17 }
因为一个赞助者可以通过多种支付方式赞助我,这句话就表明了Donator对象应该有一个PayWay的集合,因此,我们要给Donator类新加入一个集合属性,捐赠者类Donator结构如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace OneToMany.Model.Model 8 { 9 public class Donator 10 { 11 public int Id { get; set; } 12 13 public string Name { get; set; } 14 15 public string Amount { get; set; } 16 17 public DateTime DonateDate { get; set; } 18 19 /// <summary> 20 /// PayWay类型的集合属性 21 /// </summary> 22 public virtual ICollection<PayWay> PayWays { get; set; } 23 } 24 }
Donator类的配置伙伴类的定义如下:
1 using OneToMany.Model.Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Data.Entity.ModelConfiguration; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace OneToMany.Map.Map 10 { 11 public class DonatorMap :EntityTypeConfiguration<Donator> 12 { 13 public DonatorMap() 14 { 15 ToTable("Donator"); 16 //将Name设置为必须 17 this.Property(p => p.Name).IsRequired(); 18 } 19 20 } 21 }
PayWay的配置伙伴类的定义如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using OneToMany.Model.Model; 7 using System.Data.Entity.ModelConfiguration; 8 9 namespace OneToMany.Map.Map 10 { 11 public class PayWayMap : EntityTypeConfiguration<PayWay> 12 { 13 public PayWayMap() 14 { 15 ToTable("PayWay"); 16 this.Property(p => p.Name).HasMaxLength(16); 17 } 18 } 19 }
EFDbContext类定义如下:
1 using OneToMany.Model.Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Data.Entity; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace OneToMany.Map.EFContext 10 { 11 public class EFDbContext:DbContext 12 { 13 public EFDbContext() 14 : base("name=CodeFirstApplication") 15 { } 16 17 public DbSet<PayWay> PayWays { get; set; } 18 public DbSet<Donator> Donators { get; set; } 19 20 21 protected override void OnModelCreating(DbModelBuilder modelBuilder) 22 { 23 // 设置主键 24 modelBuilder.Entity<PayWay>().HasKey(p => p.PayWayId); 25 modelBuilder.Entity<Donator>().HasKey(p => p.DonatorId); 26 // 设置一对多 27 modelBuilder.Entity<Donator>().HasMany(p => p.PayWays).WithRequired(t => t.Donator); 28 base.OnModelCreating(modelBuilder); 29 } 30 } 31 }
控制台程序定义如下:
1 using OneToMany.Map.EFContext; 2 using OneToMany.Model.Model; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace OneToManyApplication 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 using (var context = new EFDbContext()) 16 { 17 var donator = new Donator 18 { 19 Amount = 6, 20 Name = "虾米", 21 DonateDate = DateTime.Now, 22 PayWays = new List<PayWay> { 23 new PayWay{Name="支付宝"}, 24 new PayWay{Name="微信"} 25 26 } 27 }; 28 context.Donators.Add(donator); 29 context.SaveChanges(); 30 } 31 32 Console.WriteLine("执行成功"); 33 Console.ReadKey(); 34 } 35 } 36 }
程序运行后数据库结构如下:
查询数据: