如何在EF Core中实例化DbContext

问题描述 投票:14回答:3

我也设置了.net核心项目和数据库上下文。但我不能开始使用dbContext但由于此错误 -

“没有给出符合所需形式参数'选项'的论据”

控制器:

public IActionResult Index()
{
    using (var db = new BlexzWebDb())
    {

    }
    return View();
}

Dbcontext代码:

public class BlexzWebDb : DbContext
{
    public BlexzWebDb(DbContextOptions<BlexzWebDb> options)
       : base(options)
    { }

    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<AssignedRole> AssignedRoles { get; set; }

}

错误图片附。是什么可能解决这个问题?提前致谢

pic

c# asp.net-core .net-core entity-framework-core
3个回答
24
投票

在EF Core中,将一些DbContextOptions传递给构造函数是很常见的。

所以一般来说,构造函数看起来像这样:

public BlexzWebDb(DbContextOptions<BlexzWebDb> options) : base(options)

正如你在那里看到的那样,没有参数构造函数形式的有效重载:

因此,这不起作用:

using (var db = new BlexzWebDb())

代替


.Net Core has IoC implemented in it's roots. Okay, this means; you don't create a context, you ask the framework to give you one, based on some rules you defined before.

示例:在某处您将注册dbcontext,(Startup.cs):

//typical configuration part of .net core
public void ConfigureServices(IServiceCollection services)
{
    //some mvc 
    services.AddMvc();

    //hey, options! 
    services.AddDbContextPool<BlexzWebDb>(options => 
           options.UseSqlServer(Configuration.GetConnectionString("BlexzWebConnection")));
    //...etc

现在注册部分已完成,您可以从框架中检索您的上下文。例如:通过控制器中的构造函数反转控制:

public class SomeController : Controller
{
    private readonly BlexzWebDb _db;

    //the framework handles this
    public SomeController(BlexzWebDb db)
    {
        _db = db;
    }

    //etc.


update

目前,将DbContext添加到服务集合的首选方法是使用AddDbContextPool方法:

//hey, options!
//hey, DbContextPool
services.AddDbContextPool<BlexzWebDb>(options => 
       options.UseSqlServer(Configuration.GetConnectionString("BlexzWebConnection")));

//etc

有关更多信息,请参阅msdn


13
投票

除了@Stefan的回答之外,还有另一种方法可以实现这一目标。您可以在DbContext类的OnConfiguring方法中设置db连接字符串,而无需在startup.cs中添加DbContext服务。

Setting.cs

public static class Setting
{
    public static string ConnectionString { get; set; }
}

Startup.cs

Setting.ConnectionString = Configuration.GetSection("ConnectionStrings:BlexzDbConnection").Value;

BlexzWebDb.cs

public class BlexzWebDb : DbContext 
{
   protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   {
       if (!optionsBuilder.IsConfigured)
       {
           optionsBuilder.UseSqlServer(Setting.ConnectionString);
       }
    }
}

HomeController.cs

public class HomeController : Controller
{
    private readonly BlexzWebDb db;

    public HomeController()
    {
        this.db = new BlexzWebDb();
    }

    //etc.

9
投票

Instantiate new object of DbContext from ConnectionString

var connectionstring = "Connection string";

var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlServer(connectionstring);

ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options);
© www.soinside.com 2019 - 2024. All rights reserved.