EF model displaying multiple databases

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

How can i do this? Do I need to create another model? I do not want, but I will, if necessary.

+4
source share
3 answers

The same model cannot receive data from two different databases. The easiest way is to create a view in the same database that calls and returns data from another database, that is, an abstraction view that internally invokes the appearance of the database.

+3
source

If your database supports synonyms, you can sync another database and combine the edmx definition with the 1st database. I wrote how to do it here

Basically you get two edmx and script files that combine the two into a working edmx file. Synonyms are used to refer to one database from another, without requiring a full path to the database.

+2
source

If you are using the first code approach in the Entity Framework, here's how to map an EF object to a table from another database:

The SQL Script that must be run in your database to create a synonym for a table from another database:

CREATE SYNONYM OtherDatabaseTableSynonym FOR otherdatabase.dbo.otherdatabasetable 

Display Entity Structure in (Fluent API):

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

Source: https://habr.com/ru/post/1339243/


All Articles