0

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.

Plarent Haxhidauti
  • 275
  • 1
  • 4
  • 17
  • 2
    Yes it is possible. Here is a link to a question that should help. Create another `DbContext`, specify it in `Startup.cs`, add the context in `appsettings.json` https://stackoverflow.com/questions/43767933/entity-framework-core-using-multiple-dbcontexts – jaabh Sep 18 '20 at 16:33

1 Answers1

0

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.

Plarent Haxhidauti
  • 275
  • 1
  • 4
  • 17