您好,登錄后才能下訂單哦!
這篇文章主要介紹Entity Framework如何管理一對(duì)二實(shí)體關(guān)系,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
要在數(shù)據(jù)庫(kù)中配置一對(duì)多關(guān)系,我們可以依賴EF約定,還可以使用數(shù)據(jù)注解或Fluent API來(lái)顯式創(chuàng)建關(guān)系。接下來(lái)使用捐贈(zèng)者Donator和支付方法PayWay這兩個(gè)類來(lái)舉例子,這里的一對(duì)多關(guān)系是:一個(gè)人可以通過(guò)多種支付方式贊助我。
支付方式類PayWay結(jié)構(gòu)如下:
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; } } }
因?yàn)橐粋€(gè)贊助者可以通過(guò)多種支付方式贊助我,這句話就表明了Donator對(duì)象應(yīng)該有一個(gè)PayWay的集合,因此,我們要給Donator類新加入一個(gè)集合屬性,捐贈(zèng)者類Donator結(jié)構(gòu)如下:
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設(shè)置為必須 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) { // 設(shè)置主鍵 modelBuilder.Entity<PayWay>().HasKey(p => p.PayWayId); modelBuilder.Entity<Donator>().HasKey(p => p.DonatorId); // 設(shè)置一對(duì)多 modelBuilder.Entity<Donator>().HasMany(p => p.PayWays).WithRequired(t => t.Donator); base.OnModelCreating(modelBuilder); } } }
控制臺(tái)程序定義如下:
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("執(zhí)行成功"); Console.ReadKey(); } } }
程序運(yùn)行后數(shù)據(jù)庫(kù)結(jié)構(gòu)如下:
查詢數(shù)據(jù):
以上是“Entity Framework如何管理一對(duì)二實(shí)體關(guān)系”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。