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);
                }
            }
        }
 public void SaveChanges()
 {
     _runner.RunAction(() => _poshContext.SaveChanges());
 }
Example #3
0
 public List <T> ToList()
 {
     UpdateIQueryable();
     return(_runner.RunAction(() => _baseIQueryable.ToList()));
 }