2
I need to use the connection to two databases, using Entity Framework, on appsettings.json
, I added this way:
"ConnectionStrings": {
"banco1": "caminhobanco1",
"banco2": "caminhobanco2"
},
and in the startup.cs
, I do like this:
public void ConfigureServices(IServiceCollection services)
{
// Configurando o uso da classe de contexto para acesso às
// tabelas do ASP.NET Identity Core
services.AddDbContext<ApplicationDbContext>
(options => options.UseSqlServer(Configuration.GetConnectionString("banco1")));
services.AddDbContext<ApplicationDbContextBanco2>
(options =>
options.UseSqlServer(Configuration.GetConnectionString("banco2")));
But when I add this way, it returns me the following error in ApplicationDbContext
:
System.Invalidoperationexception: 'The Dbcontextoptions passed to the Applicationdbcontext constructor must be a Dbcontextoptions. When registering Multiple Dbcontext types make sure that the constructor for each context type has a Dbcontextoptions Parameter rather than a non-generic Dbcontextoptions Parameter.'
The error, it returns here:
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
But if I take the startup line off the bank2, it works normal, even before adding the two banks, it worked normal. What am I doing wrong? Is there some other way, a better way, or a correction to it ?
I thought this would not occur as they were in different folders, really the error did not occur anymore !! Thank you very much, I was breaking my head here, because as they were in different folders, I thought there were no conflicts.
– Mariana
Hi @Mariana this has nothing to do with different folders or even
namespace
different, that is equal instance and why generic ai was created to solve that instance part of that class.– novic
Thanks for the explanation!!!
– Mariana