EFCore+MSSS CodeFirst多对多设计初体验
近期VS2017發(fā)布,EFCore更新到1.1,看了網(wǎng)上一篇博客:ASP.NET EntityFrameworkCore code first 多對多設(shè)計
?便想自己體驗一番。
場景:使用ASP.NET EntityFrameworkCore?CODE FIRST 創(chuàng)建多對多實體
需求:CODE FIRST實現(xiàn)多對多的實體創(chuàng)建。
細(xì)節(jié):
創(chuàng)建兩個實體類,一個是AppUser,一個是AppRole,兩個實體通過UserRole關(guān)聯(lián)。即一個AppUser可能隸屬于多個AppRole,一個AppRole可能關(guān)聯(lián)了多個AppUser。
在EntityFrameworkCore?中,不支持兩個實體之間的直接多對多,可以通過引入第三個實體,分別進(jìn)行兩次一對多來間接實現(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.
具體實現(xiàn)步驟如下:
1.VS2017創(chuàng)建項目,這里貼出具體的步驟,更直觀明了(注意截圖中的文字)。
注意:如果項目存儲路徑含有中文,報錯如下:由于找不到源文件“D:\������?\??\ASP.NET\ConsoleApp.NewDbByDp2\ConsoleApp.NewDbByDp2\Migrations\20170318Migration.cs”,因此無法添加鏈接
2.使用程序包控制臺安裝程序包
在控制臺PM后依次輸入如下兩行語句,添加程序號。
Install-Package Microsoft.EntityFrameworkCore.SqlServerInstall-Package Microsoft.EntityFrameworkCore.Tools3.在項目Models文件夾下創(chuàng)建三個類:AppUser、AppRole、UserRole(UserRole連接AppUser和AppRole實體,實現(xiàn)多對多)
代碼如下:
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.創(chuàng)建數(shù)據(jù)上下文類:ApiContext
在Models文件夾下創(chuàng)建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 實現(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 });//實現(xiàn)一個AppUser對多個AppRolemodelBuilder.Entity<UserRole>().HasOne(userrole => userrole.AppUser).WithMany(user => user.UserRoles).HasForeignKey(userrole => userrole.AppUserID);//實現(xiàn)一個AppRole對多個AppUsermodelBuilder.Entity<UserRole>().HasOne(userrole => userrole.AppRole).WithMany(role => role.UserRoles).HasForeignKey(userrole => userrole.AppRoleID);}//配置SqlServer數(shù)據(jù)庫//程序包管理器控制臺輸入 Add-Migration ApiMigration會在生成相應(yīng)的數(shù)據(jù)庫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該條語句執(zhí)行完成后,會在項目中生成一個Migration文件夾,文件夾下包含兩個cs文件,如下所示。
6.接著在程序包管理器控制臺中輸入更新數(shù)據(jù)庫語句,如下:
Update-Database這時候,控制臺會出現(xiàn)相應(yīng)的數(shù)據(jù)庫執(zhí)行語句
執(zhí)行完成后查看數(shù)據(jù)庫,可以發(fā)現(xiàn)已經(jīng)生成了ApiDb數(shù)據(jù)庫。
查看UserRole表的外鍵關(guān)系。
至此,使用EFCore CodeFirst已經(jīng)實現(xiàn)了實體類的多對多映射關(guān)系。
?
轉(zhuǎn)載于:https://www.cnblogs.com/Med1tator/p/6573066.html
總結(jié)
以上是生活随笔為你收集整理的EFCore+MSSS CodeFirst多对多设计初体验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 170318 11:44:26 [ERR
- 下一篇: 初识费用流 模板(spfa+slf优化)