Neo
Neo

Reputation: 16239

HOw can I access two database in single .edmx file?

I have created mvc3 application.

I have one .edmx already created which is based on Db1 but now I have created a view which is based on Database2 and I need to use this view inside my project.

For that I need to update my EF .edmx file. but when I right click and select option Update model from Database i'm only getting all tables , view ,sps fromDb1` its obvious But as i need to use view which is fromDatabase2how can i add it into my model.edmx` file? please help.

Upvotes: 0

Views: 3090

Answers (5)

Sofia M
Sofia M

Reputation: 1

Using ctx As New Entity()

        ctx.Database.Connection.ConnectionString = conString

End Using

Upvotes: 0

Mian
Mian

Reputation: 231

What I have done , created stored procedure in db A and accessed the db B through that SP , say select * from db2.table.then create a function import for that particular SP .

This approach works well if you have both databases on same server. In case these are on different servers you can create Linked Server on B to access A using the same stored procedure approach.

Upvotes: 0

Praveen Kumar
Praveen Kumar

Reputation: 21

If two edmx want to merge then make partial class same for both edmx file (there will be two designer classes). Add another constructor and make it parametrized, for other edmx file. Parameter to identify which edmx want to load.

Add another class file in Business layer create object of edmx partial class in this class file, Under this class when ever you want to load whom so ever edmx file pass some argument in constructor of edmx partial class constructor to identify which connection needs to open.

Pass parameter in constructor of edmx designer class, based on decided page name (custom logic or table name; That edmx will get loaded.

In web config file multiple connection strings will available for multiple edmx file.

Upvotes: 2

cincura.net
cincura.net

Reputation: 4150

One "hack" might be, for i.e. MS SQL to link these two servers and expose the data from other one on first one, i.e. via view. But I think it's manageable only for few tables. With huge models this will be pain. Other databases (Firebird, Oracle, ...) support this in similar way.

Upvotes: 0

Faust
Faust

Reputation: 15404

Entity Framework does not support mapping more than one database to one model/.edmx file (see See : unify two models (edmx) with visual studio 2010)

So you'd need to create a separate .edmx file/model for the other database, and reference each model with separate contexts. You'll need 2 connection strings in your projects as well.

Upvotes: 1

Related Questions