简体   繁体   中英

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

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;
    //}

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 ie by using a DbContextOptionBuilder as per the documentation here . So assuming you are using a SQL Server database you can use that code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM