如何在EF Core中实例化DbContext [英] How to instantiate a DbContext in EF Core

查看:1063
本文介绍了如何在EF Core中实例化DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have setup .net core project and db context also. But i cant start using dbContext yet due this error-


没有给定对应于所需正式形式
参数的参数'options'

"there is no argument given that corresponds to the required formal parameter 'options'"

控制器:

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

    }
    return View();
}

Dbcontext代码:

Dbcontext Code:

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

}

附有错误图片。该问题的可能解决方法是什么?
预先感谢

error picture attached. Whats the possible fix for that issue? Thanks in advance

< img src = https://i.stack.imgur.com/d1Px7.png alt = pic>

推荐答案

更新



这些天,向服务集合添加DbContext的首选方法是使用 AddDbContextPool 方法:

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

//etc

有关更多信息,请参见 msdn



在EF Core中,通常将一些DbContextOptions传递给构造函数。

for more info, see msdn


In EF Core it's common to pass some DbContextOptions to the constructor.

因此,通常,构造函数如下所示:

So in general, a constructor looks like this:

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

如您所见,没有参数构造函数的形式没有有效的重载:

As you can see there, there is no valid overload in the form of a parameter-less constructor:

因此,这不起作用:

using (var db = new BlexzWebDb())

相反



.Net Core的根源实现了IoC。好的,这意味着;您不创建上下文,而是要求框架根据您之前定义的一些规则为您提供上下文。

Instead


.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):

Example: somewhere you will register your 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

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

Now the registering part is done, you can retrieve your context from the framework. E.g.: inversion of control through a constructor in your controller:

public class SomeController : Controller
{
    private readonly BlexzWebDb _db;

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

    //etc.






为什么?



那么,为什么不只提供参数和 new 呢?

显然也会工作。但是,控制反转被认为是一种好习惯。当使用 asp dotnet core 时,应该经常使用它,因为大多数库都提供使用它的扩展方法。

Obviously that will work as well. But, Inversion Of Control is considered to be a good practice. When doing asp dotnet core you should use it quite often because most libraries provide extension methods to use it.

因此,而不是提供只是一种实例化方法 对象,而是尝试使您进入正确的轨道-与框架内联。之后,它将为您节省一些麻烦。此外,否则使用激活程序的CreateInstance 与答案一样有效;-)

Therefore, instead of providing "just a way to instantiate" the object, I'll try to get you onto the right track - inline with the framework. It will save you some hassle afterwards. Besides, otherwise "use an activator's CreateInstance" would just be as valid as an answer ;-)

某些链接:

  • MSDN Fundamentals
  • MSDN Dependency Injection
  • Wikipedia Inversion Of Control

这篇关于如何在EF Core中实例化DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆