基于EFcore 实现基本的数据库操作
0.代码关系与结构如图所示:
存在两个数据表,分别是UserInfo表、Contacts表,应用Scaffold-DbContext生成Model
创建两个文件夹,分别是Services(接口)、Implementation(接口的实现)
1.操作步骤:
a.创建IRepository接口;
b.创建RepositoryBase:IRepository 实现基本数据库操作
c.分别创建IUserInfoRepository、IContactRepository
d.分别创建UserInfoRepository、ContactRepository
e.创建IRepositoryWrapper
f.创建RepositoryWrapper : IRepositoryWrapper 用于实现依赖注入
g.依赖注入
h.构造函数注入与应用
2.创建IRepository接口:
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;namespace WebApplication24.Services
{
public interface IRepository<T>{
Task<bool> Insert(T t);Task<bool> Update(T t);Task<bool> Delete(T t);Task<IEnumerable<T>> GetAllAsync(); Task<IEnumerable<T>> GetByConditionAsync(Expression<Func<T, bool>> expression);Task<T> GetByIdAsync(object id);Task<bool> IsExistAsync(object id);Task<int> GetCountAsync(T t);Task<IDbContextTransaction> BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.Unspecified,CancellationToken cancellationToken = default);}
}
3.创建RepositoryBase:IRepository 实现基本数据库操作
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;namespace WebApplication24.Services
{
public class RepositoryBase<T> : IRepository<T> where T : class{
private readonly DbContext dbContext;public RepositoryBase(DbContext dbContext) => this.dbContext = dbContext;public async Task<bool> Insert(T entity){
bool bRet = false;if (entity == null){
return bRet;}bRet = IsEntityValid(entity);if (!bRet){
return bRet;}dbContext.Set<T>().Add(entity);return await dbContext.SaveChangesAsync() > 0;}public async Task<bool> Update(T entity){
bool bRet = false;if (entity == null){
return bRet;}bRet = IsEntityTracked(entity);if (!bRet){
return bRet;}bRet = IsEntityValid(entity);if (!bRet){
return bRet;}dbContext.Set<T>().Update(entity);return await dbContext.SaveChangesAsync() > 0;}public async Task<bool> Delete(T entity){
bool bRet = false;if (entity == null){
return bRet;}bRet = IsEntityTracked(entity);if (!bRet){
return bRet;}bRet = IsEntityValid(entity);if (!bRet){
return bRet;}dbContext.Set<T>().Remove(entity);return await dbContext.SaveChangesAsync() > 0;}public Task<IEnumerable<T>> GetAllAsync(){
return Task.FromResult(dbContext.Set<T>().AsEnumerable());}public Task<IEnumerable<T>> GetByConditionAsync(Expression<Func<T, bool>> expression){
return Task.FromResult(dbContext.Set<T>().Where(expression).AsEnumerable());}public async Task<T> GetByIdAsync(object id){
if (id == null){
return null;}return await dbContext.Set<T>().FindAsync(id);}public async Task<bool> IsExistAsync(object id){
if (id == null){
return false;}return await dbContext.Set<T>().FindAsync(id) != null;}public async Task<int> GetCountAsync(T entity){
return await dbContext.Set<T>().CountAsync();}public async Task<IDbContextTransaction> BeginTransactionAsync(IsolationLevel isolationLevel = System.Data.IsolationLevel.Unspecified, CancellationToken cancellationToken = default){
return await dbContext.Database.BeginTransactionAsync(isolationLevel, cancellationToken);}private bool IsEntityValid(T entity){
//判断entity是否是DbContext的ModelIEntityType entityType = dbContext.Model.FindEntityType(typeof(T));if (entityType == null){
return false;}//获取主键值名称string keyName = entityType.FindPrimaryKey().Properties.Select(p => p.Name).FirstOrDefault();if (string.IsNullOrEmpty(keyName)){
return false;}//获取主键类型Type keyType = entityType.FindPrimaryKey().Properties.Select(p => p.ClrType).FirstOrDefault();if (keyType == null){
return false;}//获取主键值类型的默认值object keyDefaultValue = keyType.IsValueType ? Activator.CreateInstance(keyType) : null;//获取当前主键值object keyValue = entity.GetType().GetProperty(keyName).GetValue(entity, null);if (keyDefaultValue.Equals(keyValue)){
return false;} else{
return true;}}private bool IsEntityTracked(T entity){
EntityEntry<T> trackedEntity = dbContext.ChangeTracker.Entries<T>().FirstOrDefault(o => o.Entity == entity);if (trackedEntity == null){
return false;}else{
return true;}}}
}
4.创建IUserInfoRepository、IContactRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication24.Models;namespace WebApplication24.Services
{
public interface IUserInfoRepository : IRepository<UserInfo>{
}public interface IContactRepository : IRepository<Contact>{
}
}
5.创建UserInfoRepository、ContactRepository
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication24.Models;namespace WebApplication24.Services
{
public class UserInfoRepository : RepositoryBase<UserInfo>,IUserInfoRepository{
public UserInfoRepository(DbContext dbContext):base(dbContext){
}}public class ContactRepository : RepositoryBase<Contact>,IContactRepository{
public ContactRepository(DbContext dbContext):base(dbContext){
}}
}
6.创建IRepositoryWrapper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace WebApplication24.Services
{
public interface IRepositoryWrapper{
IUserInfoRepository UserInfo{
get; }IContactRepository Contact {
get; }}
}
7.创建RepositoryWrapper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication24.Models;namespace WebApplication24.Services
{
public class RepositoryWrapper : IRepositoryWrapper{
private readonly ExampleContext _exampleContext;public RepositoryWrapper(ExampleContext exampleContext) => _exampleContext = exampleContext;public IUserInfoRepository UserInfo => new UserInfoRepository(_exampleContext);public IContactRepository Contact => new ContactRepository(_exampleContext);}
}
8.依赖注入 StartUp类
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();services.AddDbContext<ExampleContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("defaultConnection"));});services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();
}
9.构造注入
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using WebApplication24.Models;
using WebApplication24.Services;namespace WebApplication24.Controllers
{
public class HomeController : Controller{
private readonly ILogger<HomeController> _logger;private readonly IRepositoryWrapper _db;public HomeController(ILogger<HomeController> logger,IRepositoryWrapper repositoryWrapper){
_logger = logger;_db = repositoryWrapper;}public async Task<IActionResult> Index(){
IDbContextTransaction transaction = await _db.UserInfo.BeginTransactionAsync(IsolationLevel.ReadCommitted);try{
await _db.UserInfo.Delete(new UserInfo() {
Id = 4, Name = "Hanmeimei", Age = 30 });await _db.Contact.Insert(new Contact() {
Id = 40, Name = "Hanmeimei", Phone = "13155556666", Address = "Beijing" });await transaction.CommitAsync();}catch (Exception){
await transaction.RollbackAsync();}return View(); } }
}
自学EFCore,具体源码在我的Space里可以下载,共勉喽
Happy Ending