Esempio n. 1
0
 public void FineWorkDbContext_Initialize()
 {
     Database.SetInitializer(new FineWorkDbContextInitializer());
     var context = new FineWorkDbContext(LogManager.Factory);
     context.Database.Initialize(true);
     int result = context.Database.SqlQuery<int>("SELECT 1").Single();
     Assert.AreEqual(1, result);
 }
Esempio n. 2
0
        public DbSeed(FineWorkDbContext dbContext)
        {
            if (dbContext == null) throw new ArgumentNullException(nameof(dbContext));
            this.DbContext = dbContext;

            this.Accounts = dbContext.Set<AccountEntity>();
            this.Orgs = dbContext.Set<OrgEntity>();
            this.Staffs = dbContext.Set<StaffEntity>();
            this.Tasks = dbContext.Set<TaskEntity>();
            this.Partakers = dbContext.Set<PartakerEntity>();
            this.IncentiveKinds = dbContext.Set<IncentiveKindEntity>();
        }
Esempio n. 3
0
        public void Execute(DbSetupOptions options)
        {
            if (options == null) throw new ArgumentNullException(nameof(options));
            if (String.IsNullOrEmpty(options.ConnectionString))
                throw new ArgumentException("The connection string not specified.", nameof(options));

            var csTarget = options.TargetConnectionString;
            var csMaster = options.MasterConnectionString;

            var dbExists = SqlUtil.IsDatabaseExists(csMaster, options.DatabaseName);
            var loginExists = SqlUtil.IsLoginExists(csMaster, options.LoginName);
            var userExists = dbExists && SqlUtil.IsUserExists(csTarget, options.DatabaseName, options.UserName);

            if (options.IncludeDropSchemaObjects)
            {
                ExecuteWhen($"Dropping database [{options.DatabaseName}] schema objects", dbExists, () =>
                {
                    var sql = File.ReadAllText(@"DbScripts\DropSchemaObjects.sql");
                    SqlUtil.ExecuteSqlScript(csTarget, sql);
                });
            }

            if (options.IncludeDropDatabase)
            {
                ExecuteWhen($"Dropping database [{options.DatabaseName}]", dbExists, () =>
                {
                    SqlUtil.DropDatabase(csMaster, options.DatabaseName);
                });
            }

            if (options.IncludeCreateLogin)
            {
                ExecuteWhen($"Creating login [{options.LoginName}]", (!loginExists), () =>
                {
                    SqlUtil.CreateLogin(csMaster, options.LoginName);
                });
            }

            if (options.IncludeCreateDatabase)
            {
                Execute($"Creating database [{options.DatabaseName}]", () =>
                {
                    SqlUtil.CreateDatabase(csMaster, options.DatabaseDir, options.DatabaseName);
                });
            }

            if (options.IncludeCreateUser)
            {
                ExecuteWhen($"Creating database user {options.UserName}", (!userExists), () =>
                {
                    SqlUtil.CreateUser(csTarget, options.DatabaseName, options.UserName, options.LoginName);
                });
                ExecuteWhen($"Grant database user {options.UserName} with [db_owner]", (!userExists), () =>
                {
                    SqlUtil.GrantDbOwner(csTarget, options.UserName);
                });
            }

            if (options.IncludeCreateSchemaObjects)
            {
                Execute("Creating database schema objects", () =>
                {
                    var sql = File.ReadAllText(@"DbScripts\CreateSchemaObjects.sql");
                    SqlUtil.ExecuteSqlScript(csTarget, sql);
                });
            }

            if (options.IncludeSeed)
            {
                Execute("Seeding database objects", () =>
                {
                    using (var dbContext = new FineWorkDbContext(options.TargetConnectionString, false))
                    {
                        DbMiniSeed miniSeed = new DbMiniSeed(dbContext);
                        miniSeed.Seed();
                        dbContext.SaveChanges();
                    }
                });
            }

            if (options.IncludeBulkSeed)
            {
                using (var dbContext = new FineWorkDbContext(options.TargetConnectionString, true))
                {
                    var bulkSeed = new DbBulkSeed(dbContext);
                    bulkSeed.Seed();
                    dbContext.SaveChanges();
                }
            }
        }
Esempio n. 4
0
 public void FineWorkDbContext_is_connectable()
 {
     var context = new FineWorkDbContext(LogManager.Factory);
     int result = context.Database.SqlQuery<int>("SELECT 1").Single();
     Assert.AreEqual(1, result);
 }
Esempio n. 5
0
 public DbBulkSeed(FineWorkDbContext dbContext)
     : base(dbContext)
 {
 }