简体   繁体   English

实体框架代码优先,不同上下文/数据库之间的导航属性

[英]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.

相关问题 两个上下文之间的实体框架核心导航属性 - Entity Framework core navigation property between two contexts 实体框架代码首先迁移两个不同的数据库 - Entity Framework code first migrations for two different databases 实体框架代码优先 IQueryable 导航属性 - Entity Framework code-first IQueryable navigation property 如何通过实体框架代码优先方法定义导航属性 - How to define a navigation property via Entity Framework code first approach 首先使用实体​​框架代码-如何定义自定义导航属性 - Entity Framework code first - How to define a custom navigation property 通过关系表的实体框架代码优先导航属性 - Entity Framework Code First navigation property through relational table 实体框架代码第一个子导航属性null - Entity Framework Code First Child Navigation Property null 具有实体框架代码优先导航属性的ASP.Net MVC - ASP.Net MVC with Entity Framework Code First Navigation Property 实体框架代码首先迁移多个数据库 - Entity Framework code first migrations with multiple databases 如何在同一项目中使用 2 个不同的数据库架构组织 Entity Framework Core 迁移(代码优先)? - How to organize Entity Framework Core migrations (code first) with 2 different databases schema in the same project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM