尚未使用SQL Server为此DbContext .NET Core配置数据库提供程序 [英] No database provider has been configured for this DbContext .NET Core with SQL Server

查看:101
本文介绍了尚未使用SQL Server为此DbContext .NET Core配置数据库提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用这个把头撞在墙上,一直无济于事。



我刚刚开始了一个新的ASP.NET Core MVC项目。 ,我已经将这两个软件包安装/更新为2.2.0:

  Microsoft.EntityFrameworkCore.SqlServer 
Microsoft .EntityFrameworkCore.Tools

我已将项目设置为也期望.NET Core 2.2.0。 / p>

我能够在Package Manager控制台中使用此命令成功添加我的表模式,以搭建数据库,因此我知道连接字符串可以正常工作:

  Scaffold-DbContext SERVER =服务器\实例; DATABASE =数据库; UID =用户; PWD =密码; Microsoft.EntityFrameworkCore.SqlServer -OutputDir模型-Tables Table1,Table2,Table3 

创建的模型文件, DatabaseDBContext.cs 看起来像这样:

 公共部分类DatabaseDBContext:DbContext 
{
public DatabaseDBContext()
{
}

public DatabaseDBContext(DbContextOptions< DatabaseDBContext>选项)
:基本(选项)
{
}
}

这也包含一种用于检索我的脚手架数据,但不认为可用于生产环境,因此我将其注释掉:

 保护的覆盖无效OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if(!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer( SERVER = Server\\Instance; DATABASE = Database; UID = user; PWD = password;);
}
}

我在<$ c中添加了相同的连接字符串$ c> appsettings.json 文件:

  {
记录:{
IncludeScopes:否,
LogLevel:{
默认:警告
}
},
ConnectionStrings:{
DBConnString:服务器=服务器\\实例; DATABASE =数据库; UID =用户; PWD =密码;
}
}

然后我添加了 DbContext startup.cs 文件:

  public void ConfigureServices(IServiceCollection服务)
{
//添加框架服务。
services.AddMvc();

services.AddDbContext< DatabaseDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString( DBConnString))));
}

尝试为其中一个表添加新的脚手架控制器会引发此错误:


查找生成器控制器 ...

运行生成器控制器 ...

尝试在内存中编译应用程序。

尝试找出模型和DbContext的EntityFramework元数据:'TableName'



没有为此DbContext配置数据库提供程序。可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,则还应确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。



StackTrace:
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider,IDbContextOptions contextOptions,DbContext context)中的


在Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()处
在Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService [TService](IInfrastructure 1访问者)

Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1工厂)



尚未为此DbContext配置数据库提供程序。可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,则还要确保您的DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。


有人知道我在做什么错吗?

解决方案

所以我修复了它,但确实采用了about回的方式。我的新项目最初是在旧版本的.net core上。我已经更新了版本,但在更新过程中肯定有一些不喜欢的东西。我创建了一个新项目,并在2.2.0上启动了它,然后它起作用了……



上面的代码逻辑很合理。仍需要相同的软件包:

  Microsoft.EntityFrameworkCore.SqlServer 
Microsoft.EntityFrameworkCore.Tools

Startup.cs似乎大不相同,因此,如果有人看到了它们,他们可以尝试更新startup.cs代码:

 公共启动(IConfiguration配置)
{
Configuration = configuration;
}

public IConfiguration配置{ }

//运行时调用此方法。使用此方法将服务添加到容器。
public void ConfigureServices(IServiceCollection服务)
{
services.Configure< CookiePolicyOptions>(options =>
{
//此lambda决定用户是否同意不接受
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});必须是必需的Cookie。

services.AddDbContext< DatabaseDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString( DatabaseDBConnString))));

services.AddMvc()。SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

为此必须添加对startup.cs的引用:

 使用Microsoft.EntityFrameworkCore; 

AddDbContext方法要解决该问题。



执行此操作后,脚手架现在可以工作了。因此已修复,但需要我重新开始修复。


I have been banging my head against a wall with this one and have been googling to no avail.

I have just started a new ASP.NET Core MVC project, I have installed/updated my packages for these two to 2.2.0:

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools

I have set the project to expect .NET Core 2.2.0 as well.

I am able to successfully add my table schemas with this command in Package Manager console to scaffold the Database, so I know the connection string is fine/working:

Scaffold-DbContext "SERVER=Server\Instance;DATABASE=Database;UID=user;PWD=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables Table1, Table2, Table3

The created model file, DatabaseDBContext.cs looks like this:

public partial class DatabaseDBContext : DbContext
{
    public DatabaseDBContext()
    {
    }

    public DatabaseDBContext(DbContextOptions<DatabaseDBContext> options)
        : base(options)
    {
    }
}

This also contains a method that works to retrieve my scaffold data, but isn't considered safe for production use so I commented this out:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseSqlServer("SERVER=Server\\Instance;DATABASE=Database;UID=user;PWD=password;");
    }
}

I added this same connection string to the appsettings.json file:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "DBConnString": "SERVER=Server\\Instance;DATABASE=Database;UID=user;PWD=password;"
  }
}

I then added the DbContext to the startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    services.AddDbContext<DatabaseDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DBConnString")));
}

Trying to add a new scaffolded controller for one of the tables throws this error:

Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'TableName'

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

StackTrace:
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure1 accessor)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1 factory)

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

Has anyone got any clue what I am doing wrong here?

解决方案

So I fixed but it in a really roundabout way. My new project was originally on an older version of .net core. I had updated the version but there must have been something it didn't like during the update. I created a new project and started it on 2.2.0, then it worked...

The code logic was sound above. Still needed the same packages:

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools

Startup.cs seems quite different, so maybe if anyone else sees this they could try updating the startup.cs code:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDbContext<DatabaseDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseDBConnString")));

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

Had to add a reference to startup.cs for this:

using Microsoft.EntityFrameworkCore;

That was needed for the AddDbContext method to resolve.

After doing this the scaffolding now works. So it's fixed, but it required me to start over to fix it.

这篇关于尚未使用SQL Server为此DbContext .NET Core配置数据库提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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