1配置
startUp.cs public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddCors(); services.AddDbContext<MyDbContext>(option=> { option.UseSqlServer(Configuration.GetConnectionString("connection")); }); //<IRepository<>, Repository<>> // 泛型注入 services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); // 非泛型注入 services.AddScoped<IRepository, Repository>(); }
DbContext public class MyDbContext : DbContext { public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.Options.Extensions.LastOrDefault(); //.UseSqlServer(Configuration.GetConnectionString("connection") //optionsBuilder.UseSqlServer("Data Source=blog.db"); } /// <summary> /// OnModelCreating /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } public DbSet<appuserauth> appuserauth { get; set; } }
2.第一种实现
接口
public interface IRepository<T> where T : class { /// <summary> ///获取仓储上下文实例 /// </summary> //MyDbContext Context { get; } IEnumerable<T> GetConditions(Func<T, bool> predicate = null); T Get(Func<T, bool> predicate); T Get(params object[] keyValues); void Add(T entity); void AddRange(List<T> entitys); void Update(T entity); void Delete(T entity); void Delete(List<T> entitys); void SaveChanges(); }
实现
public class Repository<T> : IRepository<T> where T : class { private MyDbContext dbContext ; public MyDbContext Context => dbContext; /// <summary> /// 构造函数注入 /// </summary> /// <param name="_dbContext"></param> public Repository(MyDbContext _dbContext) { dbContext = _dbContext; } public void Add(T entity) { dbContext.Add(entity); } public void AddRange(List<T> entitys) { dbContext.AddRange(entitys); } public void Update(T entity) { dbContext.Update(entity); } public void Delete(T entity) { //dbContext.Add(entity).State = EntityState.Deleted; dbContext.Remove(entity); } public void Delete(List<T> entitys) { //dbContext.Add(entity).State = EntityState.Deleted; dbContext.RemoveRange(entitys); } public T Get(Func<T, bool> predicate) { return dbContext.Set<T>().Where(predicate).FirstOrDefault(); } public T Get(params object[] keyValues) { return dbContext.Find<T>(keyValues); } public IEnumerable<T> GetConditions(Func<T, bool> predicate = null) { if (predicate == null) { return dbContext.Set<T>().Where(l=>true); } return dbContext.Set<T>().Where(predicate); } public void SaveChanges() { dbContext.SaveChanges(); } }
3.第二种实现
接口
public interface IRepository { /// <summary> ///获取仓储上下文实例 /// </summary> //MyDbContext Context { get; } IEnumerable<T> GetConditions<T>(Func<T, bool> predicate = null) where T : class; T Get<T>(Func<T, bool> predicate) where T : class; T Get<T>(params object[] keyValues) where T : class; void Add<T>(T entity); void AddRange<T>(List<T> entitys); void Update<T>(T entity); void Delete<T>(T entity); void Delete<T>(List<T> entitys); void SaveChanges(); }
实现
public class Repository : IRepository { private MyDbContext dbContext; public MyDbContext Context => dbContext; /// <summary> /// 构造函数注入 /// </summary> /// <param name="_dbContext"></param> public Repository(MyDbContext _dbContext) { dbContext = _dbContext; } public void Add<T>(T entity) { dbContext.Add(entity); } public void AddRange<T>(List<T> entitys) { dbContext.AddRange(entitys); } public void Update<T>(T entity) { dbContext.Update(entity); } public void Delete<T>(T entity) { dbContext.Remove(entity); } public void Delete<T>(List<T> entitys) { dbContext.RemoveRange(entitys); } public T Get<T>(Func<T, bool> predicate) where T : class { return dbContext.Set<T>().Where(predicate).FirstOrDefault(); } public T Get<T>(params object[] keyValues) where T : class { return dbContext.Find<T>(keyValues); } public IEnumerable<T> GetConditions<T>(Func<T, bool> predicate = null) where T : class { if (predicate == null) { return dbContext.Set<T>().Where(l => true); } return dbContext.Set<T>().Where(predicate); } public void SaveChanges() { dbContext.SaveChanges(); } }