在項目中使用EF Core還是比較容易的,在這裏我們使用的版本是EF Core 2.2.
1.使用nuget獲取EF Core包
這個示例項目使用的是SQLSERVER,所以還需要下載Microsoft.EntityFrameworkCore.SqlServer這個包
2.在Startup類的Configure方法中設置默認的數據庫訪問連接字符串
//數據庫連接字符串 Framework.Core.Configuration.AddItem("ConnectionStrings",Configuration.GetSection("ConnectionStrings").Value);
PS:我這裏並沒有使用DI注入的方式去使用EFCORE的實例,還是使用傳統的New的方式,所以並不需要在Startup中進行注入
3.在Mango.EFCore類庫項目中創建一個EFDbContext類繼承自DbContext我們就能在其它地方使用EFCore了,代碼如下:
using System; using System.Collections.Generic; using System.Text; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using Mango.Entity; namespace Mango.Framework.EFCore { public class EFDbContext : DbContext { public EFDbContext() { } public EFDbContext(DbContextOptions<EFDbContext> options): base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(Core.Configuration.GetItem("ConnectionStrings")); } } #region Entity DbSet<> public virtual DbSet<m_WebSiteConfig> m_WebSiteConfig { get; set; } public virtual DbSet<m_PostsRecords> m_PostsRecords { get; set; } public virtual DbSet<m_WebSiteNavigation> m_WebSiteNavigation { get; set; } public virtual DbSet<m_Sms> m_Sms { get; set; } public virtual DbSet<m_ManagerAccount> m_ManagerAccount { get; set; } public virtual DbSet<m_ManagerMenu> m_ManagerMenu { get; set; } public virtual DbSet<m_ManagerPower> m_ManagerPower { get; set; } public virtual DbSet<m_ManagerRole> m_ManagerRole { get; set; } public virtual DbSet<m_Message> m_Message { get; set; } public virtual DbSet<m_Navigation> m_Navigation { get; set; } public virtual DbSet<m_NavigationClassify> m_NavigationClassify { get; set; } public virtual DbSet<m_Posts> m_Posts { get; set; } public virtual DbSet<m_PostsChannel> m_PostsChannel { get; set; } public virtual DbSet<m_PostsAnswer> m_PostsAnswer { get; set; } public virtual DbSet<m_PostsAnswerRecords> m_PostsAnswerRecords { get; set; } public virtual DbSet<m_PostsAttention> m_PostsAttention { get; set; } public virtual DbSet<m_PostsComments> m_PostsComments { get; set; } public virtual DbSet<m_PostsCommentsRecords> m_PostsCommentsRecords { get; set; } public virtual DbSet<m_PostsTags> m_PostsTags { get; set; } public virtual DbSet<m_User> m_User { get; set; } public virtual DbSet<m_UserGroup> m_UserGroup { get; set; } public virtual DbSet<m_UserGroupMenu> m_UserGroupMenu { get; set; } public virtual DbSet<m_UserGroupPower> m_UserGroupPower { get; set; } #endregion } }
4.接下來我們在Mango.Repository倉儲類庫項目中使用EFCore,代碼示例如下:
using System; using System.Collections.Generic; using System.Text; using System.Linq; using Mango.Framework.EFCore; using Microsoft.EntityFrameworkCore; using System.Data.SqlClient; namespace Mango.Repository { public class AuthorizationRepository { private EFDbContext _dbContext = null; public AuthorizationRepository() { _dbContext = new EFDbContext(); } /// <summary> /// 根據用戶組獲取權限 /// </summary> /// <param name="GroupId"></param> /// <returns></returns> public List<Models.UserGroupPowerModel> GetPowerData(int groupId) { var query = from ugp in _dbContext.m_UserGroupPower join ugm in _dbContext.m_UserGroupMenu on ugp.MId equals ugm.MId where ugp.GroupId == groupId select new Models.UserGroupPowerModel() { GroupId=ugp.GroupId.Value, MId=ugm.MId.Value, MName=ugm.MName, AreaName=ugm.AreaName, ControllerName=ugm.ControllerName, ActionName=ugm.ActionName }; return query.ToList(); } } }
以上介紹了EFCore的基本使用示例,其實我們平常在項目中會將一些常用的增刪改統一封裝起來,我們創建一個CommonRepository類,代碼如下:
using System; using System.Reflection; using System.Collections.Generic; using System.Text; using Mango.Framework.EFCore; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; namespace Mango.Repository { public class CommonRepository { private EFDbContext _dbContext = null; public CommonRepository() { _dbContext = new EFDbContext(); } /// <summary> /// 添加記錄 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <returns></returns> public bool Add<TEntity>(TEntity entity) where TEntity:class { _dbContext.Add(entity); return _dbContext.SaveChanges() > 0; } /// <summary> /// 根據Id獲取指定記錄 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="Id"></param> /// <returns></returns> public TEntity Find<TEntity>(int Id) where TEntity : class { return _dbContext.Find<TEntity>(Id); } /// <summary> /// 更新記錄 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <param name="IsFind"></param> /// <returns></returns> public bool Update<TEntity>(TEntity entity, bool isFind) where TEntity : class { _dbContext.Update(entity); return _dbContext.SaveChanges() > 0; } /// <summary> /// 更新記錄(修改指定的列) /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <param name="properties"></param> /// <returns></returns> public bool Update<TEntity>(TEntity entity) where TEntity : class { _dbContext.Entry(entity).State = EntityState.Unchanged; // Type type= entity.GetType(); //處理實體類屬性 PropertyInfo[] properties = type.GetProperties(); foreach (var property in properties) { object value = property.GetValue(entity, null); var key = property.GetCustomAttribute<KeyAttribute>(); if (value != null&& key==null) { _dbContext.Entry(entity).Property(property.Name).IsModified = true; } } return _dbContext.SaveChanges() > 0; } /// <summary> /// 刪除記錄 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <returns></returns> public bool Delete<TEntity>(TEntity entity) where TEntity : class { _dbContext.Remove(entity); return _dbContext.SaveChanges() > 0; } } }
PS:這篇EFCore的基礎使用就到此爲止,詳情請下載源代碼查看,下一篇將講解如何基於EFCore進行一些基礎的擴展