C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver

1配置

C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver
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>();
}
View Code
C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver
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; }

    }
View Code

2.第一种实现

接口

C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver
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();

    }
View Code

实现

C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver
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();
        }

        
    }
View Code

3.第二种实现

接口

C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver
 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();

    }
View Code

实现

C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver
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();
        }


    }
View Code

 

C# 基于ef的2种简单的仓储封装 net core3.1 ---sqlserver

上一篇:SQLServer字符串查找(判断字符串是否含数字或字母)


下一篇:Windows下Mysql5.7忘记root密码的解决方法