Two error connection in the Entity Framework class constructor?

Asked

Viewed 205 times

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 ?

1 answer

3


In the injection of addiction, there is a conflict, he cannot decide which is the DbContextOptions which is to instantiate, an easy way to solve this is to type the DbContextOptions<> so that it knows which is to be passed in the constructor of these classes, example:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
    : base(options)
{
}

public ApplicationDbContext2(DbContextOptions<ApplicationDbContext2> options) 
    : base(options)
{
}

Note that google error snippet translation means: O construtor ApplicationDbContext deve ser um DbContextOptions. Ao registrar vários tipos DbContext, certifique-se de que o construtor de cada tipo de contexto tenha um parâmetro DbContextOptions em vez de um parâmetro DbContextOptions não genérico. ', in the error itself is saying that it is to use the generic instead of the non-generic, and the class DbContextOptions has generic and non-generic and has been created so for these instances of conflict.

  • 1

    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.

  • 1

    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.

  • Thanks for the explanation!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.