New Posts New Posts RSS Feed: Multiple DataSource in a Single Model
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Multiple DataSource in a Single Model

 Post Reply Post Reply
Author
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Topic: Multiple DataSource in a Single Model
    Posted: 06-Dec-2011 at 7:37pm
Hi,

I believe Devforce has a way to direct a single Entity in a model to connect to a different datasource in runtime while the remaining Entities in the model connects to a different datasource as well.

Example I defined DomainModel w/c consists of 4 entities:

1. ABC - this entity connects to Global DB 
2. DEF - this entity connects to Product DB
3. XYZ - this entity connects to Product DB
4. VON - this entity connects to Product DB

And then I put association between ABC and DEF.

How can I build the connection in runtime programmatically?

Thanks,
Von


Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Posted: 06-Dec-2011 at 8:09pm
Taken from your page:

Multiple data sources

The Entity Framework only supports one database per Entity Data Model. DevForce lets you define a single domain model that holds entities from multiple data sources and use navigation properties to move seamlessly between them. Saving back to these data sources is just as seamless as DevForce can encapsulates all of the changes into a single transaction. 
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 07-Dec-2011 at 6:31pm
Hi Von;

You can dynamically build a connection string at runtime by implementing IDataSourceKeyResolver. Please see the following pages and let me know if you have further questions.

DataSourceKeys and DataSourceExtensions discussion - http://drc.ideablade.com/xwiki/bin/view/Documentation/data-sources

Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Posted: 07-Dec-2011 at 9:05pm
Hi Denis,

I believe the one you send is for programmatically changing the connection in runtime.
But my question is is there a possibility that within a single Model, like in my example.

1. ABC - this entity connects to Global DB 
2. DEF - this entity connects to Product DB
3. XYZ - this entity connects to Product DB
4. VON - this entity connects to Product DB

And then I put association between ABC and DEF.

Entities connects to different datasource, what I mean is that, when I query ABC and DEF in a single query expression, it will 
access different database.

var query = context.ABC.Select(p => new {
ComesFrom_GlobalDB = p.DEF.columnHere,
ComesFrom_ProductDB = p.columnHere
   }
);

Like in this example, ABC should connect to GlobalDB and DEF should connect to ProductDB.
How can I set different connections per entity in runtime?

Thanks,
Von
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 08-Dec-2011 at 4:01pm
Hi Von;

Sorry for the misunderstanding. I'm afraid we don't support setting different connections per entity in runtime.

However, there is a workaround that will enable you to do the above.

It requires a specific setup as follows:

1. Create a new edmx, say GlobalDB.edmx that will contain entity ABC and points to GlobalDB

2. Create another edmx, ProductDB.edmx that will contain entity DEF, XYZ and VON and points to ProductDB

3. In your ABC partial class, create the association between ABC and DEF as follows.

Assuming the relationship is as follows DEF (1) to (many) ABC
DEF - DEF_id - PK

ABC - ABC_id - PK
ABC - DEF_id - FK to DEF

public DEF DEF {
  get {
    return this.EntityManager.DEF.Where(e => e.DEF_Id == this.DEF_Id).FirstOrNullEntity(); 
  }
}

4. In your DEF partial class, create the association between ABC and DEF as follows.

Assuming the relationship is as follows DEF (1) to (many) ABC
DEF - DEF_id - PK

ABC - ABC_id - PK
ABC - DEF_id - FK to DEF

public IList<ABC> ABCs {
  get {
    return this.EntityManager.ABC.Where(e => e.DEF_id == this.DEF_Id).ToList();
  }
}

In other words, the association is not defined in the edmx but rather in the entity partial class in code.

Hope this helps.
Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Posted: 08-Dec-2011 at 4:59pm
Ok I'll try to do it.
Btw, does it mean that in GlobalDB.edmx project I should have a reference to Product.edmx in case of different project or they should reside on the same assembly?

Thanks,
Von
Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Posted: 08-Dec-2011 at 5:01pm
Hi Denis,

I'm just curious what does it mean by this?
I thought I could create single consolidated model with entities coming from multiple datasource such as databases, different databases, WCF services and etc.

Multiple data sources

The Entity Framework only supports one database per Entity Data Model. DevForce lets you define a single domain model that holds entities from multiple data sources and use navigation properties to move seamlessly between them. Saving back to these data sources is just as seamless as DevForce can encapsulates all of the changes into a single transaction.

Thanks,
Von
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 08-Dec-2011 at 6:37pm
It's recommended to have both edmx in the same DomainModel assembly.

A single domain model in this case means a single EntityManager. In your example, GlobalDB.edmx and ProductDB.edmx can have the same EntityManager as long as they're in the same namespace and assembly.

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down