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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

EFCore+MSSS CodeFirst多对多设计初体验

發布時間:2025/7/25 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EFCore+MSSS CodeFirst多对多设计初体验 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

近期VS2017發布,EFCore更新到1.1,看了網上一篇博客:ASP.NET EntityFrameworkCore code first 多對多設計

?便想自己體驗一番。

場景:使用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創建項目,這里貼出具體的步驟,更直觀明了(注意截圖中的文字)。

注意:如果項目存儲路徑含有中文,報錯如下:由于找不到源文件“D:\������?\??\ASP.NET\ConsoleApp.NewDbByDp2\ConsoleApp.NewDbByDp2\Migrations\20170318Migration.cs”,因此無法添加鏈接

2.使用程序包控制臺安裝程序包

在控制臺PM后依次輸入如下兩行語句,添加程序號。

Install-Package Microsoft.EntityFrameworkCore.SqlServerInstall-Package Microsoft.EntityFrameworkCore.Tools

3.在項目Models文件夾下創建三個類:AppUser、AppRole、UserRole(UserRole連接AppUser和AppRole實體,實現多對多)

代碼如下:  

using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace ManyToMany.Models {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 List<UserRole> UserRoles { get; set; }} } AppUser類 using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace ManyToMany.Models {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類 using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace ManyToMany.Models {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.創建數據上下文類:ApiContext

在Models文件夾下創建ApiContext類,添加引用using Microsoft.EntityFrameworkCore,完整類代碼如下:

using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace ManyToMany.Models {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 });//實現一個AppUser對多個AppRolemodelBuilder.Entity<UserRole>().HasOne(userrole => userrole.AppUser).WithMany(user => user.UserRoles).HasForeignKey(userrole => userrole.AppUserID);//實現一個AppRole對多個AppUsermodelBuilder.Entity<UserRole>().HasOne(userrole => userrole.AppRole).WithMany(role => role.UserRoles).HasForeignKey(userrole => userrole.AppRoleID);}//配置SqlServer數據庫//程序包管理器控制臺輸入 Add-Migration ApiMigration會在生成相應的數據庫protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlServer(@"Server=DP_PC\DPSQL2008;Initial Catalog=ApiDb;Persist Security Info=True;User ID=sa;Password=xx;");}} } ApiContext類

5.在程序包管理器控制臺中輸入如下語句:

Add-Migration ApiMigration

該條語句執行完成后,會在項目中生成一個Migration文件夾,文件夾下包含兩個cs文件,如下所示。

6.接著在程序包管理器控制臺中輸入更新數據庫語句,如下:

Update-Database

這時候,控制臺會出現相應的數據庫執行語句

執行完成后查看數據庫,可以發現已經生成了ApiDb數據庫。

查看UserRole表的外鍵關系。

至此,使用EFCore CodeFirst已經實現了實體類的多對多映射關系。

?

轉載于:https://www.cnblogs.com/Med1tator/p/6573066.html

總結

以上是生活随笔為你收集整理的EFCore+MSSS CodeFirst多对多设计初体验的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。