How to connect to two different database in ef core database first

c# entity-framework-core

Question

How to create two different database context to use two different database in EF core. i am using database first approach.

Currently using the following line to use in my startup.cs

builderModel.Services.AddDbContext<DB1Context>(options =>
            options.UseSqlServer(configuration.GetConnectionString("DB1Context")));

Thanks

1
0
2/24/2020 1:35:16 PM

Popular Answer

Sounds pretty straight forward (if DBs are already set up and equal in schema). Try

builderModel.Services.AddDbContext<DB1Context>(options =>
            options.UseSqlServer(configuration.GetConnectionString("DB1Context")));

builderModel.Services.AddDbContext<DB2Context>(options =>
            options.UseSqlServer(configuration.GetConnectionString("DB2Context")));

And as a bonus, you can inherit from the existing DbContext you already have (because it's the same code).

2
2/24/2020 1:56:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow