这篇文章主要介绍Entity Framework如何管理一对二实体关系,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
要在数据库中配置一对多关系,我们可以依赖EF约定,还可以使用数据注解或Fluent API来显式创建关系。接下来使用捐赠者Donator和支付方法PayWay这两个类来举例子,这里的一对多关系是:一个人可以通过多种支付方式赞助我。
支付方式类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; } /// <summary> /// PayWay类型的集合属性 /// </summary> public virtual ICollection<PayWay> 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<Donator> { 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<PayWay> { 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<PayWay> PayWays { get; set; } public DbSet<Donator> Donators { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // 设置主键 modelBuilder.Entity<PayWay>().HasKey(p => p.PayWayId); modelBuilder.Entity<Donator>().HasKey(p => p.DonatorId); // 设置一对多 modelBuilder.Entity<Donator>().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<PayWay> { new PayWay{Name="支付宝"}, new PayWay{Name="微信"} } }; context.Donators.Add(donator); context.SaveChanges(); } Console.WriteLine("执行成功"); Console.ReadKey(); } }}
程序运行后数据库结构如下:
查询数据:
以上是“Entity Framework如何管理一对二实体关系”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!