示例#1
0
        public PoshEntityQueryBase(DbContext context, ActionRunner runner, string WhereQuery = "", List <object> whereParams = null, string fromSql = "", List <object> fromSqlParams = null)
        {
            _runner = runner;
            var ets = context.Model.GetEntityTypes();

            foreach (var et in ets)
            {
                if (et.ClrType == typeof(T))
                {
                    if (et.IsQueryType)
                    {
                        _baseIQueryable = context.Query <T>().AsQueryable();
                    }
                    else
                    {
                        _baseIQueryable = context.Set <T>().AsQueryable();
                    }
                }
            }

            _baseContext = context;
            _query       = WhereQuery;
            if (whereParams == null)
            {
                _whereParams = new List <object>();
            }
            else
            {
                _whereParams = whereParams;
            }

            _fromSql       = fromSql;
            _fromSqlParams = fromSqlParams;
        }
        public void NewDbContext <T>(
            string connectionString,
            string dbType,
            bool EnsureCreated,
            bool ReadOnly,
            PoshEntity[] Types = null
            )
            where T : DbContext
        {
            _runner = new ActionRunner(Credential);
            var dbOptions           = new DbContextOptionsBuilder <T>();
            IServiceCollection coll = new ServiceCollection();

            switch (dbType.ToUpper())
            {
            case "SQLITE":
                dbOptions.UseSqlite(connectionString);
                coll = coll.AddEntityFrameworkSqlite();
                break;

            case "MSSQL":
            default:
                dbOptions.UseSqlServer(connectionString);
                coll = coll.AddEntityFrameworkSqlServer();
                break;
            }
            var sp = BuildServiceProvider(coll);

            dbOptions.UseInternalServiceProvider(sp);

            DbContext dbContext = null;

            if (typeof(T) == typeof(PoshContext))
            {
                dbContext = new PoshContext(dbOptions.Options, Types);
            }
            else
            {
                dbContext = (DbContext)Activator.CreateInstance(typeof(T), new object[] { dbOptions.Options });
            }
            if (EnsureCreated)
            {
                _runner.RunAction(() => dbContext.Database.EnsureCreated());
            }
            if (ReadOnly)
            {
                dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
            }
            _poshContext = dbContext;
            foreach (var t in Types)
            {
                if (!String.IsNullOrEmpty(t.FromSql))
                {
                    FromSqlEntities.Add(t.Type.Name, t.FromSql);
                }
            }
        }
示例#3
0
 public PoshEntityQuery(string columnName, ActionRunner runner, string WhereQuery, DbContext dbContext, List <object> whereParams, string fromSql, List <object> fromSqlParams)
 {
     _runner        = runner;
     _whereQuery    = WhereQuery;
     _columnName    = columnName;
     _dbContext     = dbContext;
     _whereParams   = whereParams;
     _fromSql       = fromSql;
     _fromSqlParams = fromSqlParams;
 }
示例#4
0
 public PoshEntityColumn(DbContext dbContext, ActionRunner runner, string WhereQuery, List <object> whereParams, string fromSql, List <object> fromSqlParams) : base(dbContext, runner, WhereQuery, whereParams, fromSql, fromSqlParams)
 {
 }