[英]Entity Framework Code First, Navigation Property between different contexts/databases
Hi I have 2 data contexts that maps different schemas on a SQL Server database, but then I need to create 1 sdf database file (SQL Compact) per schema and use the same data contexts, and I have some entities related like this: 嗨,我有2个数据上下文在SQL Server数据库上映射不同的架构,但是我需要为每个架构创建1个sdf数据库文件(SQL Compact)并使用相同的数据上下文,并且我有一些相关的实体,如下所示:
//context 1
class A
{
int Id
...
ICollection<B> Bs
}
//context 2
class B
{
int Id
...
}
On server it's easy I just need to specify the table for this relation, but on clients I have this entities splitted on different databases. 在服务器上,只需要为此关系指定表就很容易了,但是在客户端上,我将这个实体拆分到了不同的数据库中。
So I need a navigation property on 1 entity (A) from context 1 (database_A.sdf) to relate with 1 entity (B) from context 2 (database_B.sdf). 因此,我需要上下文1(database_A.sdf)的1个实体(A)的导航属性,以与上下文2(database_B.sdf)的1个实体(B)关联。
Thanks in advance. 提前致谢。
Anwering my own question, It's not possible to do what I need, because one context only can link to only one database, one way to do it it's to have an database attached like in SQLite, but with SQL Compact it's impossible. 在回答我自己的问题时,无法做我需要做的事情,因为一个上下文只能链接到一个数据库,一种实现方式是像在SQLite中那样连接数据库,但是使用SQL Compact是不可能的。
Sources: SQLite - How do you join tables from different databases? 资料来源: SQLite-如何连接来自不同数据库的表? SQL Compact 3.5 attach multiple DB/ cross-db query? SQL Compact 3.5附加多个DB /跨数据库查询吗?
Those classes you are implemented are not Contexts
those are Entities
. 您实现的那些类不是Entities
Contexts
。 The Context in EF should inherit from ObjectContext or DbContext, In your case I think you have 2 separate Entities in 2 different Database. EF中的上下文应继承自ObjectContext或DbContext,就您而言,我认为您在2个不同的数据库中有2个单独的实体。 You can do this to pointing several database 您可以这样做以指向多个数据库
// Associate with first entity
public Context1 : ObjectContext
{
prop IDbSet<A> ADbSet{ get; set; }
...
}
// Associate with Second entity
public Context2 : ObjectContext
{
prop IDbSet<B> BDbSet{ get; set; }
...
}
public void ChangeDb(string dbName)
{
Context1 context = new Context1();
context.ChangeDatabase(dbName);
}
Hope this help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.