C# Problem with DbContext: Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'

c# database dbcontext entity-framework-core string


I' ve got some problem with DbContext. I decided to make new class, which will be responsible for connections with DataBase.

public class PetAlertContext : DbContext
    public PetAlertContext() : base("PetAlert") { }

    public DbSet<Zwierze> Zwierzaki { get; set; }
        public DbSet<Osoba> Osoby { get; set; }
        public DbSet<Placowka> Placowki { get; set; }
        public DbSet<Ogloszenie> Ogloszenia { get; set; }

But I have some exception near the name of base:

Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'.

I found similiar issue and response with some code like this, but I have a little problem with understanding this construction.

Would You be so kind to help me with that?

//public PetAlertContext(string connectionString) : base(GetOptions(connectionString))

    //private static DbContextOptions GetOptions(string connectionString)
    //    return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
9/29/2019 9:54:23 PM

Accepted Answer

The constructor for Entity Framework Core does not take a single string as a parameter, it takes a DbContextOptions instance. See the documentation here. The constructor you are currently trying to use is the EF6 constructor which does take just the connection string as the parameter (see here). The code you have commented out in your question shows you how to instantiate an instance of DbContextOptions i.e. by using a DbContextOptionBuilder as per the documentation here. So assuming you are using a SQL Server database you can use that code.

9/29/2019 9:48:18 PM

Related Questions


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