OL.FrameCore.Infrastructure/UnitOfWork/EFUnitOfWork.cs

(开头部分) 4KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace OL.FrameCore.Infrastructure.UnitOfWork
{
    public class EFUnitOfWork<TContext> : IUnitOfWork<TContext> where TContext : DbContext
    {
        TContext _dbContext;
        Dictionary<Type, object> dictRepository;

        public EFUnitOfWork(TContext dbContext)
        {
            _dbContext = dbContext;
        }

        public TContext DbContext => _dbContext;

        public IRepositoryBase<TEntity> GetRepository<TEntity>() where TEntity : class
        {
            //var repository = new RepositoryBase<TEntity>(_dbContext);
            //return repository;
            if (dictRepository == null)
            {
                dictRepository = new Dictionary<Type, object>();
            }
            Type type = typeof(TEntity);
            if (!this.dictRepository.ContainsKey(type))
            {
                this.dictRepository[type] = new RepositoryBase<TEntity>(this._dbContext);
            }
            return (IRepositoryBase<TEntity>)this.dictRepository[type];
            
        }

        public void Save()
        {
            _dbContext.SaveChanges();
        }

        //public void ExecuteSqlQuery(string sql)
        //{
        //    _dbContext.Database.SqlQuery<UserInfo>("select* from UserInfoes ").ToList();
        //}
        public IList<T> SqlQuery<T>(string sql, params object[] parameters)
            where T : new()
        {
            //注意:不要对GetDbConnection获取到的conn进行using或者调用Dispose,否则DbContext后续不能再进行使用了,会抛异常
            var conn = _dbContext.Database.GetDbConnection();
            try
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    command.CommandText = sql;
                    command.Parameters.AddRange(parameters);
                    var propts = typeof(T).GetProperties();
                    var rtnList = new List<T>();
后面还有 54 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。