5

I've got some problem with DbContext. I decided to make a 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 a similar 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? I commented these out:

public PetAlertContext(string connectionString) : base(GetOptions(connectionString))
{
}

private static DbContextOptions GetOptions(string connectionString)
{
    return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
}
totymedli
  • 29,531
  • 22
  • 131
  • 165
KlaudynaZ
  • 55
  • 1
  • 1
  • 5

1 Answers1

12

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.

strickt01
  • 3,959
  • 1
  • 17
  • 32