Exemple #1
0
        public void LoadEntityDescriptors()
        {
            var entityTypes = Options.DbModuleOptions.EntityTypes;

            if (entityTypes != null && entityTypes.Any())
            {
                foreach (var entityType in entityTypes)
                {
                    EntityDescriptorCollection.Add(new EntityDescriptor(this, entityType));
                }
            }
        }
Exemple #2
0
        public NetSqlQueryable(IDbSet <TEntity> dbSet, Expression <Func <TEntity, bool> > whereExpression) : base(dbSet, new QueryBody(dbSet.DbContext.Options.SqlAdapter))
        {
            QueryBody.JoinDescriptors.Add(new QueryJoinDescriptor
            {
                Type             = JoinType.UnKnown,
                Alias            = "T1",
                EntityDescriptor = EntityDescriptorCollection.Get <TEntity>(),
            });
            QueryBody.WhereDelegateType = typeof(Func <,>).MakeGenericType(typeof(TEntity), typeof(bool));

            Where(whereExpression);
        }
Exemple #3
0
        public NetSqlQueryable(IDbSet <TEntity> dbSet, Expression <Func <TEntity, bool> > whereExpression, string tableName = null, bool noLock = true) : base(dbSet, new QueryBody(dbSet.DbContext.Options.SqlAdapter))
        {
            QueryBody.JoinDescriptors.Add(new QueryJoinDescriptor
            {
                Type             = JoinType.UnKnown,
                Alias            = "T1",
                EntityDescriptor = EntityDescriptorCollection.Get <TEntity>(),
                TableName        = tableName.NotNull() ? tableName : Db.EntityDescriptor.TableName,
                NoLock           = noLock
            });

            Where(whereExpression);
        }
        public NetSqlQueryable(IDbSet dbSet, QueryBody queryBody, Expression <Func <TEntity, TEntity2, TEntity3, bool> > onExpression, JoinType joinType = JoinType.Left) : base(dbSet, queryBody)
        {
            Check.NotNull(onExpression, nameof(onExpression), "请输入连接条件");

            QueryBody.JoinDescriptors.Add(new QueryJoinDescriptor
            {
                Type             = joinType,
                Alias            = "T3",
                EntityDescriptor = EntityDescriptorCollection.Get <TEntity3>(),
                On = onExpression
            });

            QueryBody.WhereDelegateType =
                typeof(Func <, , ,>).MakeGenericType(typeof(TEntity), typeof(TEntity2), typeof(TEntity3), typeof(bool));
        }
Exemple #5
0
        protected DbContext(IDbContextOptions options, IServiceProvider serviceProvider)
        {
            Options         = options;
            ServiceProvider = serviceProvider;
            LoginInfo       = Options.LoginInfo;

            if (options.DbOptions.CreateDatabase)
            {
                if (options.DatabaseCreateEvents != null)
                {
                    options.DatabaseCreateEvents.DbContext = this;
                }

                options.SqlAdapter.CreateDatabase(EntityDescriptorCollection.Get(options.DbModuleOptions.Name), options.DatabaseCreateEvents);
            }
        }
Exemple #6
0
        public NetSqlQueryable(IDbSet dbSet, QueryBody queryBody, Expression <Func <TEntity, TEntity2, bool> > onExpression, JoinType joinType = JoinType.Left, string tableName = null, bool noLock = false)
            : base(dbSet, queryBody)
        {
            Check.NotNull(onExpression, nameof(onExpression), "请输入连接条件");
            var t2 = new QueryJoinDescriptor
            {
                Type             = joinType,
                Alias            = "T2",
                EntityDescriptor = EntityDescriptorCollection.Get <TEntity2>(),
                On     = onExpression,
                NoLock = noLock
            };

            t2.TableName = tableName.NotNull() ? tableName : t2.EntityDescriptor.TableName;
            QueryBody.JoinDescriptors.Add(t2);
        }
        public NetSqlQueryable(IDbSet dbSet, QueryBody queryBody, Expression <Func <TEntity, TEntity2, bool> > onExpression, JoinType joinType = JoinType.Left, string tableName = null)
            : base(dbSet, queryBody)
        {
            Check.NotNull(onExpression, nameof(onExpression), "请输入连接条件");
            var t2 = new QueryJoinDescriptor
            {
                Type             = joinType,
                Alias            = "T2",
                EntityDescriptor = EntityDescriptorCollection.Get <TEntity2>(),
                On = onExpression
            };

            t2.TableName = tableName.NotNull() ? tableName : t2.EntityDescriptor.TableName;
            QueryBody.JoinDescriptors.Add(t2);

            QueryBody.WhereDelegateType = typeof(Func <, ,>).MakeGenericType(typeof(TEntity), typeof(TEntity2), typeof(bool));
        }
        public NetSqlQueryable(IDbSet dbSet, QueryBody queryBody, Expression <Func <TEntity, TEntity2, TEntity3, TEntity4, TEntity5, TEntity6, TEntity7, TEntity8, TEntity9, TEntity10, bool> > onExpression, JoinType joinType = JoinType.Left, string tableName = null, bool noLock = true)
            : base(dbSet, queryBody)
        {
            Check.NotNull(onExpression, nameof(onExpression), "请输入连接条件");

            var t10 = new QueryJoinDescriptor
            {
                Type             = joinType,
                Alias            = "T10",
                EntityDescriptor = EntityDescriptorCollection.Get <TEntity10>(),
                On     = onExpression,
                NoLock = noLock
            };

            t10.TableName = tableName.NotNull() ? tableName : t10.EntityDescriptor.TableName;
            QueryBody.JoinDescriptors.Add(t10);
        }
Exemple #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="name">连接名称</param>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="sqlAdapter">数据库适配器</param>
        /// <param name="loggerFactory">日志工厂</param>
        protected DbContextOptionsAbstract(string name, string connectionString, ISqlAdapter sqlAdapter, ILoggerFactory loggerFactory)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name), "数据库连接名称未配置");
            }

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString), "数据库连接字符串未配置");
            }

            Name                       = name;
            ConnectionString           = connectionString;
            SqlAdapter                 = sqlAdapter;
            EntityDescriptorCollection = new EntityDescriptorCollection(SqlAdapter, new EntitySqlBuilder());
            LoggerFactory              = loggerFactory;
        }
        public NetSqlQueryable(IDbSet dbSet, QueryBody queryBody, Expression <Func <TEntity, TEntity2, TEntity3, TEntity4, TEntity5, TEntity6, TEntity7, TEntity8, TEntity9, TEntity10, bool> > onExpression, JoinType joinType = JoinType.Left, string tableName = null)
            : base(dbSet, queryBody)
        {
            Check.NotNull(onExpression, nameof(onExpression), "请输入连接条件");

            var t10 = new QueryJoinDescriptor
            {
                Type             = joinType,
                Alias            = "T10",
                EntityDescriptor = EntityDescriptorCollection.Get <TEntity10>(),
                On = onExpression,
            };

            t10.TableName = tableName.NotNull() ? tableName : t10.EntityDescriptor.TableName;
            QueryBody.JoinDescriptors.Add(t10);

            QueryBody.WhereDelegateType = typeof(Func <, , , , , , , , , ,>).MakeGenericType(typeof(TEntity), typeof(TEntity2), typeof(TEntity3),
                                                                                             typeof(TEntity4), typeof(TEntity5), typeof(TEntity6), typeof(TEntity7), typeof(TEntity8), typeof(TEntity9), typeof(TEntity10), typeof(bool));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dbOptions"></param>
        /// <param name="options"></param>
        /// <param name="sqlAdapter">数据库适配器</param>
        /// <param name="loggerFactory">日志工厂</param>
        /// <param name="loginInfo">登录信息</param>
        protected DbContextOptionsAbstract(DbOptions dbOptions, DbModuleOptions options, ISqlAdapter sqlAdapter, ILoggerFactory loggerFactory, ILoginInfo loginInfo)
        {
            if (options.Name.IsNull())
            {
                throw new ArgumentNullException(nameof(options.Name), "数据库连接名称未配置");
            }

            DbOptions       = dbOptions;
            DbModuleOptions = options;
            SqlAdapter      = sqlAdapter;
            LoggerFactory   = loggerFactory;
            LoginInfo       = loginInfo;

            if (options.EntityTypes != null && options.EntityTypes.Any())
            {
                foreach (var entityType in options.EntityTypes)
                {
                    EntityDescriptorCollection.Add(new EntityDescriptor(dbOptions, options, entityType, sqlAdapter));
                }
            }
        }
Exemple #12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="options"></param>
        /// <param name="sqlAdapter">数据库适配器</param>
        /// <param name="loggerFactory">日志工厂</param>
        /// <param name="httpContextAccessor"></param>
        protected DbContextOptionsAbstract(DbConnectionOptions options, ISqlAdapter sqlAdapter, ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor)
        {
            if (options.Name.IsNull())
            {
                throw new ArgumentNullException(nameof(options.Name), "数据库连接名称未配置");
            }

            if (options.ConnString.IsNull())
            {
                throw new ArgumentNullException(nameof(options.ConnString), "数据库连接字符串未配置");
            }

            Name                = options.Name;
            ConnectionString    = options.ConnString;
            SqlAdapter          = sqlAdapter;
            LoggerFactory       = loggerFactory;
            HttpContextAccessor = httpContextAccessor;

            foreach (var entityType in options.EntityTypes)
            {
                EntityDescriptorCollection.Add(new EntityDescriptor(entityType, sqlAdapter, new EntitySqlBuilder()));
            }
        }
Exemple #13
0
 public void CreateDatabase()
 {
     Options.SqlAdapter.CreateDatabase(EntityDescriptorCollection.Get(Options.DbModuleOptions.Name), Options.DatabaseCreateEvents, out bool exists);
     DatabaseExists = exists;
 }