2

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:

//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).

Thanks in advance.

Pedro Simões
  • 237
  • 2
  • 10

2 Answers2

4

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.

Sources: SQLite - How do you join tables from different databases? SQL Compact 3.5 attach multiple DB/ cross-db query?

Community
  • 1
  • 1
Pedro Simões
  • 237
  • 2
  • 10
1

Those classes you are implemented are not Contexts those are Entities. The Context in EF should inherit from ObjectContext or DbContext, In your case I think you have 2 separate Entities in 2 different Database. 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.

Saber Amani
  • 6,409
  • 12
  • 53
  • 88