How can I implement DbContext Connection String in .NET Core?

.net c# entity-framework


I'm trying to figure out how to use this identical function in.NET Core syntax since my circumstance is fairly similar to this link, or at least my code is comparable.

Give code-first DbContext your connection string.

The code I use is as follows:

public partial class CompanyFormsContext : DbContext
    public CompanyFormsContext()
        : base("name=CompanyFormsContext")

    public CompanyFormsContext(string connName)
        : base("name=" + connName)

A message stating:

Error CS1503 Argument 1: cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions' CompanyForms..NETCoreApp,Version=v1.0

Upon reviewing the parentheses inbase("name=CompanyFormsContext") or base("name=" = connName) .

How should this capability be implemented properly in.NET Core?


I wanted to let you know that in my appsettings.json file, I have the following information for the database connection: (However, I do not have settings in the startup.cs)

  "Data": {
    "CompanyFormsContext": {
      "ConnectionString": "Server=(localdb)\\projectsv13;Database=companyforms;Trusted_Connection=True;"
    "CompanyFormsContextQA": {
      "ConnectionString": "Server=(localdb)\\projectsv13;Database=companyforms;Trusted_Connection=True;"

and I discovered the internet URL DbContextOptions added to Startup.cs fail to register data storage, and I'm wondering whether a straightforwardprotected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) if it will be enough to repair my connection.

Using the link

        options =>

Do I need such a service for my Startup.cs?

5/23/2017 12:10:39 PM

Accepted Answer

Calling the basic function Object() { [native code] }, which accepts a DbContextOptions, is another choice.

public BooksContext(string connectionString) : base(GetOptions(connectionString))

private static DbContextOptions GetOptions(string connectionString)
    return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
11/28/2018 6:14:24 AM

Popular Answer


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