數據結構圖以下:css
這次實例比較簡單,暫時只設計到上述3張表數據庫
SMUser:用於存儲用戶信息。
Role:用於存儲角色信息。
SMUser_Role:用創建用戶和角色關係的一直關聯表。數據結構
開發工具:visual studio 2015
打開vs2015->新建項目->.NET Core->ASP.NET Core Application(.Net core)
以下圖:數據庫設計
給本身的項目取個名字,選個路徑,就完事了。
而後在本身建立的解決方案裏再新增個類庫項目,此類庫項目用於實現數據庫的交互,也是實現EF Core的地方,以下圖:ide
DAL項目使用Nuget添加如下引用:工具
Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools
在DAL項目中新建Entities文件夾,該文件夾用於創建與數據庫表一一對應的實體類。咱們根據數據庫結構,建立一下3個實體類。
SMUser:開發工具
using System; using System.Collections.Generic; namespace SnmiOA.DAL.Entities { public class SMUser { public Guid SMUserId { get; set; } public string SSOUserName { get; set; } public string SSOPassword { get; set; } public string TrueName { get; set; } public bool IsValid { get; set; } public string Mobile { get; set; } public string Email { get; set; } public string UserNo { get; set; } public string EmployeeNo { get; set; } public string QQ { get; set; } public virtual ICollection<SMUserRole> SMUserRoles { get; set; } } }
Role:ui
using System; using System.Collections.Generic; namespace SnmiOA.DAL.Entities { public class Role { public Guid RoleId { get; set; } public string RoleName { get; set; } public int OrderField { get; set; } public virtual ICollection<SMUserRole> SMUserRoles { get; set; } } }
SMUserRole:spa
using System; namespace SnmiOA.DAL.Entities { public class SMUserRole { public Guid SMUserId { get; set; } public Guid RoleId { get; set; } public virtual Role Role { get; set; } public virtual SMUser SMUser { get; set; } } }
在DAL項目下添加SnmiOAContext.cs文件。其代碼以下:設計
public class SnmiOAContext : DbContext { public SnmiOAContext(DbContextOptions<SnmiOAContext> options) : base(options) { } public DbSet<SMUser> SMUsers { get; set; } public DbSet<Role> Roles { get; set; } public DbSet<SMUserRole> SMUserRoles { get; set; } }
而後咱們須要添加一下3張表之間的映射關係,經過表結構能夠看出來,實際上咱們的SMUser和Role之間是多對多的關係,SMUser_Role是兩張表產生的一張中間表,在之前的EF中這兩張表能夠直接映射多對多的關係。可是在EF Core中目前我尚未發現這種映射關係的寫法,多是我閱讀的資料還不夠,也多是真的沒有提供這種映射。後來我就找到個把他們都分別改爲一對多的關係來寫,發現也是能夠的。代碼以下:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<SMUserRole>() .ToTable("SMUser_Role") .HasKey(ur => new { ur.RoleId, ur.SMUserId }); modelBuilder.Entity<SMUserRole>() .HasOne(ur => ur.SMUser) .WithMany(u => u.SMUserRoles) .HasForeignKey(ur => ur.SMUserId); modelBuilder.Entity<SMUserRole>() .HasOne(ur => ur.Role) .WithMany(r => r.SMUserRoles) .HasForeignKey(ur => ur.RoleId); modelBuilder.Entity<SMUser>() .ToTable("SMUser") .HasKey(u => u.SMUserId); modelBuilder.Entity<SMUser>() .HasMany(u => u.SMUserRoles) .WithOne(ur => ur.SMUser) .HasForeignKey(u => u.SMUserId); modelBuilder.Entity<Role>() .ToTable("Role") .HasKey(r => r.RoleId); modelBuilder.Entity<Role>() .HasMany(r => r.SMUserRoles) .WithOne(ur => ur.Role) .HasForeignKey(ur => ur.RoleId); }
若是你們有更好的方法,還請告知,謝謝!
最後,別忘記了DBContext的依賴注入。
咱們在APP項目的StartUp文件的ConfigureServices方法中添加如下代碼:
services.AddDbContext<SnmiOAContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SnmiOAConnection")));
總體看上去應該是這樣:
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); services.AddDbContext<SnmiOAContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SnmiOAConnection"))); services.AddMvc(); }
Repository實現
當咱們使用不一樣的數據模型和領域模型時,倉儲模式特別有用。倉儲能夠充當數據模型和領域模型之間的中介。在內部,倉儲以數據模型的形式和數據庫交互,而後給數據訪問層之上的應用層返回領域模型。
在咱們這個例子中,由於使用了數據模型做爲領域模型,所以,也會返回相同的模型。若是想要使用不一樣的數據模型和領域模型,那麼須要將數據模型的值映射到領域模型或使用任何映射庫執行映射。
如今定義倉儲接口IRepository以下:
using System; using System.Linq; using System.Linq.Expressions; namespace SnmiOA.DAL.Repository { public interface IRepository<T> where T :class { IQueryable<T> GetAllList(Expression<Func<T, bool>> predicate = null); T Get(Expression<Func<T, bool>> predicate); void Insert(T entity); void Delete(T entity); void Update(T entity); long Count(); } }
上面的幾個方法都是常見的CRUD操做,就不解釋了.
而後再實現一個倉儲類的泛型基類,用來實現IRepository接口,代碼以下:
using Microsoft.EntityFrameworkCore; using System; using System.Linq; using System.Linq.Expressions; namespace SnmiOA.DAL.Repository { public class RepositoryBase<T> : IRepository<T> where T : class { private readonly SnmiOAContext _context = null; private readonly DbSet<T> _dbSet; public RepositoryBase(SnmiOAContext context) { _context = context; _dbSet = _context.Set<T>(); } public long Count() { return _dbSet.LongCount(); } public void Delete(T entity) { _dbSet.Remove(entity); } public T Get(Expression<Func<T, bool>> predicate) { return _dbSet.FirstOrDefault(predicate); } public IQueryable<T> GetAllList(Expression<Func<T, bool>> predicate = null) { if (predicate == null) { return _dbSet; } return _dbSet.Where(predicate); } public void Insert(T entity) { _dbSet.Add(entity); } public void Update(T entity) { _dbSet.Attach(entity); _context.Entry(entity).State = EntityState.Modified; } } }
這樣每一個實體類的倉儲類實現起來,就很是簡單了,以下:
using SnmiOA.DAL.Entities; namespace SnmiOA.DAL.Repository { public class RoleRepository : RepositoryBase<Role> { public RoleRepository(SnmiOAContext context) : base(context) { } } }
再安裝上述代碼分別爲SMUser和SMUserRole創建倉儲類,若是須要更復雜的數據庫查詢操做,能夠上上述倉儲類中補充實現。
咱們已經知道,DbContext默認支持事務,當實例化一個新的DbContext對象時,就會建立一個新的事務,當調用SaveChanges方法時,事務會提交。問題是,若是咱們使用相同的DbContext對象把多個代碼模塊的操做放到一個單獨的事務中,該怎麼辦呢?答案就是工做單元(Unit of Work)。
工做單元本質是一個類,它能夠在一個事務中跟蹤全部的操做,而後將全部的操做做爲原子單元執行。看一下倉儲類,能夠看到DbContext對象是從外面傳給它們的。此外,全部的倉儲類都沒有調用SaveChanges方法,緣由在於,咱們在建立工做單元時會將DbContext對象傳給每一個倉儲。當想保存修改時,就能夠在工做單元上調用SaveChanges方法,也就在DbContext類上調用了SaveChanges方法。這樣就會使得涉及多個倉儲的全部操做成爲單個事務的一部分。
這裏定義咱們的工做單元類以下:
using SnmiOA.DAL.Repository; using System; namespace SnmiOA.DAL { public class UnitOfWork : IDisposable { private readonly SnmiOAContext _context = null; private SMUserRepository _userRepository = null; private SMUserRoleRepository _userRoleRepository = null; private RoleRepository _roleRepository = null; public UnitOfWork(SnmiOAContext context) { _context = context; } public SMUserRepository SMUserRepository { get { return _userRepository ?? (_userRepository = new SMUserRepository(_context)); } } public SMUserRoleRepository SMUserRoleRepository { get { return _userRoleRepository ?? (_userRoleRepository = new SMUserRoleRepository(_context)); } } public RoleRepository RoleRepository { get { return _roleRepository ?? (_roleRepository = new RoleRepository(_context)); } } public void SaveChanges() { _context.SaveChanges(); } public void Dispose() { throw new NotImplementedException(); } } }
完