实体框架和多个模式 [英] Entity Framework and multiple schemas
问题描述
我试图建立我的DbContext,以便它可以处理单个Oracle数据库多种模式。我不想一个整体的DbContext文件,所以我想出了以下内容:
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 following:
public class oraDbContext : DbContext
{
static oraDbContext() {
Database.SetInitializer<oraDbContext>(null);
}
public oraDbContext(string connName)
: base("Name=" + connName) { }
public _schema1 schema1 = _schema1.Instance;
public _schema2 schema2 = _schema2.Instance;
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
schema1.OnModelCreating(modelBuilder);
schema2.OnModelCreating(modelBuilder);
}
}
模式文件看起来是这样的:
The schema file looks like this:
public sealed class _schema1
{
private static readonly _schema1 instance = new _schema1();
static _schema1() { }
private _schema1() { }
public static _schema1 Instance {
get {
return instance;
}
}
public DbSet<someTable> someTable { get; set; }
internal void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Configurations.Add(new someTableMap());
}
}
然而,当我试图执行一个查询我得到的错误:值不能为空
。它所指的值是_schema1在 someTable
属性。
However, when I try to perform a query I get the error: Value cannot be null
. The value it's referring to is the someTable
property in _schema1.
一个。我该如何解决这个问题?
A. How can I fix this?
乙。有没有更好的解决办法?
B. Is there a better solution?
编辑:我想在这里写code的能力,比如下面 -
What I want here is the ability to write code such as the following -
var query1 = from p in db.schema1.someTable
select p;
var query2 = from p in db.schema2.someTable
select p;
在哪里someTable是两种模式是相同的。在我们的数据库中,我们有几个模式与具有相同或几乎相同的列完全相同的表。我不想为每个模式一个单独的DbContext,因为这可能意味着5种不同的连接,如果我创建,从5模式拉查询。如果我在写SQL直同一查询,我可以拉5种不同模式的数据与单个连接,这就是我想在这里完成的。
Where someTable is the same in both schemas. In our database we have several schemas with the exact same tables that have identical or nearly identical columns. I don't want to create a seperate dbContext for each schema because that could potentially mean 5 different connections if I'm creating a query that pulls from 5 schemas. If I was writing this same query in straight SQL I could pull the data from 5 different schemas with a single connection and that's what I would like to accomplish here.
推荐答案
虽然做一些研究关于实体框架我碰上了以下职位:
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-$c$c-first/
这并不完全给我一个单一的DbContext一起工作,但它只能使用一个连接(这是我的身后不想使用多个dbContexts推理)。设置以下code之后:
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<oraDbContext>(null);
}
private oraDbContext(DbConnection connection, DbCompiledModel model)
: base(connection, model, contextOwnsConnection: false) { }
public DbSet<SomeTable1> SomeTable1 { get; set; }
public DbSet<SomeTable2> SomeTable2 { get; set; }
private static ConcurrentDictionary<Tuple<string, string>, DbCompiledModel> modelCache = new ConcurrentDictionary<Tuple<string, string>, 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<SomeTable1>(new SomeTable1Map(schemaName));
builder.Configurations.Add<SomeTable2>(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<DailyDependencyTable>
{
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())
}
}
&NBSP;
这篇关于实体框架和多个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!