khr055
khr055

Reputation: 29032

EF model mapping multiple databases

I have a model in my project that maps to a LOT of views in my database, but I need to map to a view in another database.

How can I do this? Do I have to create another model? I don't want to, but I will if I have to.

Upvotes: 4

Views: 1741

Answers (3)

Dmitry Komin
Dmitry Komin

Reputation: 559

If you use code first approach in Entity Framework, here is how to map EF entity to the table from other database:

SQL Script that needs to be run in your database to create synonym for the table from other database:

CREATE SYNONYM OtherDatabaseTableSynonym FOR otherdatabase.dbo.otherdatabasetable

Entity Framework Mapping in (Fluent API):

modelBuilder.Entity<OtherDatabaseTableEntity>().ToTable("OtherDatabaseTableSynonym").HasKey(x => x.id);

Upvotes: 0

Rachel
Rachel

Reputation: 132548

If your database supports synonyms, you could setup a synonym to the other database, and merge the edmx definition in with your 1st database's definition. I wrote how to do it here

Basically you end up with two edmx files, and a script that merges the two into a working edmx file. Synonyms are used to reference one database from the other without needing the full database path.

Upvotes: 2

amit_g
amit_g

Reputation: 31250

The same model can't get data from the two different DBs. The easiest way would be to create a view in the same database that calls and returns data from the other database i.e. the abstraction view that internally calls external DB view.

Upvotes: 3

Related Questions