日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET EntityFrameworkCore code first 多对多设计

發(fā)布時間:2025/3/21 asp.net 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET EntityFrameworkCore code first 多对多设计 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 摘要:參考網(wǎng)址:https://docs.microsoft.com/zh-cn/ef/core/get-started/full-dotnet/new-db場景:使用ASP.NETEntityFrameworkCoreCODEFIRST創(chuàng)建多對多實體需求:CODEFIRST實現(xiàn)多對多的實體創(chuàng)建。細節(jié):創(chuàng)建兩個實體類,一個是AppUser,一個是AppRole,兩個實體通過UserRole關(guān)聯(lián)。即一個AppUser可能隸屬于多個AppRole,一個AppRole可能關(guān)聯(lián)了多個AppUser
  • 標簽:.net?ASP.NET?Framework?net?設計

參考網(wǎng)址:https://docs.microsoft.com/zh-cn/ef/core/get-started/full-dotnet/new-db

場景:使用ASP.NET EntityFrameworkCore?CODE FIRST 創(chuàng)建多對多實體

需求:CODE FIRST實現(xiàn)多對多的實體創(chuàng)建。

細節(jié):

創(chuàng)建兩個實體類,一個是AppUser,一個是AppRole,兩個實體通過UserRole關(guān)聯(lián)。即一個AppUser可能隸屬于多個AppRole,一個AppRole可能關(guān)聯(lián)了多個AppUser。

在EntityFrameworkCore?中,不支持兩個實體之間的直接多對多,可以通過引入第三個實體,分別進行兩次一對多來間接實現(xiàn)多對多。

官方描述為:

Many-to-many?relationships without an entity?class?to represent the join table are not yet supported. However, you?can?represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.

?

步驟:

1.使用VS2017創(chuàng)建項目;

2.NuGet添加?Microsoft.EntityFrameworkCore.SqlServer

3.創(chuàng)建如下三個類

public class AppUser{public int AppUserID { get; set; }public string Guid { get; set; }public string UserName { get; set; }public string LoginName { get; set; }public string LoginPassword { get; set; }public string Phone { get; set; }public string Email { get; set; }public int Sex { get; set; }public int BranchOfficeID { get; set; }public BranchOffice BranchOffice { get; set; }public List<UserRole> UserRoles { get; set; }} logs_code_collapse">AppUser 類? public class AppRole{public int AppRoleID { get; set; }public string Guid { get; set; }public string RoleName { get; set; }public List<UserRole> UserRoles { get; set; }} AppRole類? public class UserRole{public int UserRoleID { get; set; }public int AppRoleID { get; set; }public AppRole AppRole { get; set; }public int AppUserID { get; set; }public AppUser AppUser { get; set; }} UserRole類

4.創(chuàng)建DBContext

public class ApiContext:DbContext{public DbSet<AppUser> AppUsers { get; set; }public DbSet<AppRole> AppRoles { get; set; }public DbSet<UserRole> UserRoles { get; set; }/// <summary>/// 通過第三張表UserRole 實現(xiàn) 多對多綁定 AppRole 和 AppUser/// </summary>/// <param name="modelBuilder"></param>protected override void OnModelCreating(ModelBuilder modelBuilder){modelBuilder.Entity<UserRole>().HasKey(t => new { t.AppRoleID, t.AppUserID });modelBuilder.Entity<UserRole>().HasOne(userrole => userrole.AppUser).WithMany(user => user.UserRoles).HasForeignKey(userrole => userrole.AppUserID);modelBuilder.Entity<UserRole>().HasOne(userrole => userrole.AppRole).WithMany(role => role.UserRoles).HasForeignKey(userrole=> userrole.AppRoleID);}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlServer(@"Server=sandbox.XXXX.com;Initial Catalog=API;Persist Security Info=True;User ID=sa;Password=XXXXX;");}} DbContext類

5.運行?Add-Migration APIMigration?

運行結(jié)束后,可以發(fā)現(xiàn)項目中多了 APIMigration.cs

6.運行Update-Database,生成數(shù)據(jù)庫。

檢查數(shù)據(jù)庫生成結(jié)果:

?

檢查UserRoles表的外鍵:

至此,創(chuàng)建多對多的實體成功。

總結(jié)

以上是生活随笔為你收集整理的ASP.NET EntityFrameworkCore code first 多对多设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。