コード例 #1
0
        protected static SchoolsDbContext CreateNewContext(bool enableLazyLoading = false)
        {
            var context = new SchoolsDbContext(_optionsBuilder.Options);

            context.ChangeTracker.LazyLoadingEnabled = enableLazyLoading;
            return(context);
        }
コード例 #2
0
        private void ConfigureShards(IServiceScope serviceScope)
        {
            var databaseManager      = serviceScope.ServiceProvider.GetService <IDatabaseManager>();
            var shardsConfiguration  = serviceScope.ServiceProvider.GetService <ShardsConfiguration>();
            var shardProvider        = serviceScope.ServiceProvider.GetService <IShardFactory <int> >();
            var shardMapProvider     = serviceScope.ServiceProvider.GetService <IShardMapProvider <int> >();
            var shardMappingProvider = serviceScope.ServiceProvider.GetService <IShardMappingFactory <int> >();

            databaseManager.CreateIfNotExtists(ShardMapConnectionString);

            foreach (var shardConfiguration in shardsConfiguration.Shards)
            {
                databaseManager.CreateIfNotExtists(shardConfiguration.Server, shardConfiguration.UserName, shardConfiguration.Password.ToSecureString(), shardConfiguration.Database);
                var shardMap = shardMapProvider.CreateOrGetListShardMap(ShardMapConnectionString, ShardMapName);
                var shard    = shardProvider.CreateOrGet(shardMap, shardConfiguration.Server, shardConfiguration.Database);

                foreach (var shardingKey in shardConfiguration.ShardingKeys)
                {
                    shardMappingProvider.CreateIfNotExists(shardMap, shard, shardingKey);
                }

                var optionsBuilder         = new DbContextOptionsBuilder <SchoolsDbContext>();
                var connectionStringBuiler = new SqlConnectionStringBuilder();
                connectionStringBuiler.Add("Server", shardConfiguration.Server);
                connectionStringBuiler.Add("Data Source", shardConfiguration.Database);
                connectionStringBuiler.Add("User Id", shardConfiguration.UserName);
                connectionStringBuiler.Add("Password", shardConfiguration.Password);
                optionsBuilder.UseSqlServer(connectionStringBuiler.ConnectionString,
                                            sqlServerOptions => sqlServerOptions.MigrationsAssembly(_migrationsAssembly));
                var schoolsDbContext = new SchoolsDbContext(optionsBuilder.Options);
                schoolsDbContext.Database.EnsureCreated();
            }
        }
コード例 #3
0
 public MuncipalityRepository(SchoolsDbContext dbContext) : base(dbContext)
 {
 }
コード例 #4
0
 public SchoolRepository(SchoolsDbContext dbContext) : base(dbContext)
 {
 }
コード例 #5
0
 public StudentsController(SchoolsDbContext dbContext)
 {
     _dbContext = dbContext;
 }