示例#1
0
 protected BaseRepository(IFreeSql fsql, Expression <Func <TEntity, bool> > filter, Func <string, string> asTable = null)
 {
     _ormScoped = DbContextScopedFreeSql.Create(fsql, () => _db, () => UnitOfWork);
     DataFilterUtil.SetRepositoryDataFilter(this, null);
     DataFilter.Apply("", filter);
     AsTable(asTable);
 }
示例#2
0
 public RepositoryDbContext(IFreeSql orm, IBaseRepository repo) : base()
 {
     _ormScoped       = DbContextScopedFreeSql.Create(orm, () => this, () => repo.UnitOfWork);
     _isUseUnitOfWork = false;
     UnitOfWork       = repo.UnitOfWork;
     _repo            = repo;
 }
        static IServiceCollection AddFreeDbContext(this IServiceCollection services, Type dbContextType, Action <DbContextOptionsBuilder> options)
        {
            services.AddScoped(dbContextType, sp =>
            {
                DbContext ctx = null;
                try
                {
                    var ctor       = dbContextType.GetConstructors().FirstOrDefault();
                    var ctorParams = ctor.GetParameters().Select(a => sp.GetService(a.ParameterType)).ToArray();
                    ctx            = Activator.CreateInstance(dbContextType, ctorParams) as DbContext;
                }
                catch (Exception ex)
                {
                    throw new Exception($"AddFreeDbContext 发生错误,请检查 {dbContextType.Name} 的构造参数都已正确注入", ex);
                }
                if (ctx != null && ctx._ormScoped == null)
                {
                    var builder = new DbContextOptionsBuilder();
                    options(builder);
                    ctx._ormScoped   = DbContextScopedFreeSql.Create(builder._fsql, () => ctx, () => ctx.UnitOfWork);
                    ctx._optionsPriv = builder._options;

                    if (ctx._ormScoped == null)
                    {
                        throw new Exception("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
                    }

                    ctx.InitPropSets();
                }
                return(ctx);
            });
            return(services);
        }
示例#4
0
 public UnitOfWorkManager(IFreeSql fsql)
 {
     if (fsql == null)
     {
         throw new ArgumentNullException($"{nameof(UnitOfWorkManager)} 构造参数 {nameof(fsql)} 不能为 null");
     }
     _ormScoped = DbContextScopedFreeSql.Create(fsql, null, () => this.Current);
 }
示例#5
0
 public UnitOfWorkManager(IFreeSql fsql)
 {
     if (fsql == null)
     {
         throw new ArgumentNullException(DbContextStrings.UnitOfWorkManager_Construction_CannotBeNull(nameof(UnitOfWorkManager), nameof(fsql)));
     }
     _ormScoped = DbContextScopedFreeSql.Create(fsql, null, () => this.Current);
 }
示例#6
0
        protected BaseRepository(IFreeSql fsql, Expression <Func <TEntity, bool> > filter, Func <string, string> asTable = null)
        {
            _ormScoped = DbContextScopedFreeSql.Create(fsql, () => _db, () => UnitOfWork);
            DataFilterUtil.SetRepositoryDataFilter(this, null);
            DataFilter.Apply("", filter);
            AsTable(asTable);

            fsql.GlobalFilter.GetFilters().ForEach(gf =>
            {
                (DataFilter as DataFilter <TEntity>)._filtersByOrm.TryAdd(gf.Name, new DataFilter <TEntity> .FilterItemByOrm
                {
                    Filter    = gf,
                    IsEnabled = true
                });
            });
        }