ASP.NET EntityFrameworkCore code first 多对多设计
- 摘要:參考網址:https://docs.microsoft.com/zh-cn/ef/core/get-started/full-dotnet/new-db場景:使用ASP.NETEntityFrameworkCoreCODEFIRST創建多對多實體需求:CODEFIRST實現多對多的實體創建。細節:創建兩個實體類,一個是AppUser,一個是AppRole,兩個實體通過UserRole關聯。即一個AppUser可能隸屬于多個AppRole,一個AppRole可能關聯了多個AppUser
- 標簽:.net?ASP.NET?Framework?net?設計
參考網址:https://docs.microsoft.com/zh-cn/ef/core/get-started/full-dotnet/new-db
場景:使用ASP.NET EntityFrameworkCore?CODE FIRST 創建多對多實體
需求:CODE FIRST實現多對多的實體創建。
細節:
創建兩個實體類,一個是AppUser,一個是AppRole,兩個實體通過UserRole關聯。即一個AppUser可能隸屬于多個AppRole,一個AppRole可能關聯了多個AppUser。
在EntityFrameworkCore?中,不支持兩個實體之間的直接多對多,可以通過引入第三個實體,分別進行兩次一對多來間接實現多對多。
官方描述為:
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創建項目;
2.NuGet添加?Microsoft.EntityFrameworkCore.SqlServer
3.創建如下三個類
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.創建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 實現 多對多綁定 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?
運行結束后,可以發現項目中多了 APIMigration.cs
6.運行Update-Database,生成數據庫。
檢查數據庫生成結果:
?
檢查UserRoles表的外鍵:
至此,創建多對多的實體成功。
總結
以上是生活随笔為你收集整理的ASP.NET EntityFrameworkCore code first 多对多设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ObjectInputStreamObj
- 下一篇: 你不知道的composer自动加载