Connect two data sources in ASP.NET Core app

The application I have developed now needs to connect to another database. My own database is created with migrations, but I also need to create a model that correlates to this other table in another database.

Is this possible or not. Do I have to create another Data Context class that defines the table in the other database for this other connection as well?

I’d appreciate if someone can give me a direction to look at.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

What I managed to do is the following.

In Visual Studio, select menu Tools -> NuGet Package Manger -> Package Manger Console and run this command:

Scaffold-DbContext "Server=.SQLExpress;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Basically you copy and paste your connection string to the 2nd database. The command creates the new context and model classes for each table in the database. Then in your controller create an instance of your context (same way as the 1st one), and you’re good to go.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x