Entity Framework and multiple schemas

后端 未结 3 817
执笔经年
执笔经年 2020-12-01 03:28

I\'m trying to set up my dbContext so that it can handle multiple schemas in a single Oracle database. I didn\'t want one monolithic dbContext file so I\'ve come up with the

3条回答
  •  囚心锁ツ
    2020-12-01 03:58

    While doing some research about Entity Framework I came across the following post:

    http://romiller.com/2011/05/23/ef-4-1-multi-tenant-with-code-first/

    It doesn't quite give me a single dbContext to work with but it does only use a single connection (which was my reasoning behind not wanting to use multiple dbContexts). After setting up the following code:

    public class oraDbContext : DbContext
    {
        static oraDbContext() {
            Database.SetInitializer(null);
        }
    
        private oraDbContext(DbConnection connection, DbCompiledModel model)
            : base(connection, model, contextOwnsConnection: false) { }
    
        public DbSet SomeTable1 { get; set; }
        public DbSet SomeTable2 { get; set; }
    
        private static ConcurrentDictionary, DbCompiledModel> modelCache = new ConcurrentDictionary, DbCompiledModel>();
    
        public static oraDbContext Create(string schemaName, DbConnection connection) {
            var compiledModel = modelCache.GetOrAdd(
                Tuple.Create(connection.ConnectionString, schemaName),
                t =>
                {
                    var builder = new DbModelBuilder();
                    builder.Configurations.Add(new SomeTable1Map(schemaName));
                    builder.Configurations.Add(new SomeTable2Map(schemaName));
    
                    var model = builder.Build(connection);
                    return model.Compile();
                });
    
            return new oraDbContext(connection, compiledModel);
        }
    }
    

    This of course requires that my mapping files be set up like so:

    public class DailyDependencyTableMap : EntityTypeConfiguration
    {
        public SomeTableMap(string schemaName) {
            this.ToTable("SOME_TABLE_1", schemaName.ToUpper());
    
            //Map other properties and stuff
        }
    }
    

    Writing queries that use multiple schemas is somewhat annoying but, for the moment, it does what I need it to do:

    using (var connection = new OracleConnection("a connection string")) {
        using (var schema1 = oraDbContext.Create("SCHEMA1", connection))
        using (var schema2 = oraDbContext.Create("SCHEMA2", connection)) {
    
            var query = ((from a in schema1.SomeTable1 select new { a.Field1 }).ToList())
                 .Concat((from b in schema2.SomeTable1 select new { b.Field1 }).ToList())
        }
    }
    

     

提交回复
热议问题