In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how Entity Framework manages one-to-two entity relationships. The introduction in the article is very detailed and has certain reference value. Interested friends must read it!
To configure one-to-many relationships in a database, we can rely on the EF convention or explicitly create relationships using data annotations or the Fluent API. And then we'll use the Donator and PayWay categories as an example, where the one-to-many relationship is: One person can sponsor me through multiple payment methods.
支付方式类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 Framework如何管理一对二实体关系"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.