简体   繁体   中英

ASP.NET 5 Console Application (package) - How to create DBContext from connection string?

I'm trying to create DBContext from connection string on .net core. This used to work with .net 4.5:

public class MyContext : DbContext
{
   internal MYContext(string connectionString) : base(connectionString){}
}

Now I'm getting error that the base class (DBContext) doesn't have constructor that accepts string.

Any ideas?

DbContext doesn't have this constructor anymore. You can specify the connection string in OnConfiguring method overload:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {         
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;");

    }
}

https://ef.readthedocs.org/en/latest/getting-started/full-dotnet/new-db.html

UseSqlServer is an extension method for DbContextOptionsBuilder

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