简体   繁体   English

使用SQLite'尚未为此DbContext配置数据库提供程序'

[英]'No database provider has been configured for this DbContext' using SQLite

I'm just experimenting with EF Core and SQLite and have been slightly stumped by the issue. 我只是尝试使用EF Core和SQLite,因此对此问题有些困惑。

If I attempt to configure the database like this: 如果我尝试像这样配置数据库:

services.AddDbContext<MyDbContext>(options => options.UseSqlite("Filename=./App_Data/dashboard.db") );

And then execute: 然后执行:

dotnet ef migrations add InitialCreate -c MyDbContext

I get: 我得到:

'No database provider has been configured for this DbContext' '尚未为此DbContext配置数据库提供程序'

But if I do this: 但是,如果我这样做:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlite("Filename=./App_Data/dashboard.db");
}

All works as expected. 所有工作均按预期进行。

My context is setup to work with the fluent interface: 我的上下文设置为使用流畅的界面:

public MyDbContext(DbContextOptions options) 
    : base(options)
{
}

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

It seems from various articles that others have the fluent approach working fine. 从各种文章看来,其他人都可以使用流利的方法。

Have anyone else had this issue and solved in using Core 2.1.0? 还有其他人遇到过此问题并在使用Core 2.1.0时解决了吗?

I would try adding a design time db context factory to your project like this: 我会尝试向您的项目中添加一个设计时数据库上下文工厂,如下所示:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace yournamespace
{
    public class YourDbContextDesignTimeFactory : IDesignTimeDbContextFactory<YourDbContext>
    {
        public YourDbContext CreateDbContext(string[] args)
        {
            var builder = new DbContextOptionsBuilder<YourDbContext>();
            builder.UseSqlite("Data Source=somename.db");

            return new YourDbContext(builder.Options);
        }
    }
}

I don't think the connection string has to be correct, EF will use this to create the context when generating the migration 我认为连接字符串不一定正确,EF在生成迁移时将使用此字符串创建上下文

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尚未为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序 - InvalidOperationException: No database provider has been configured for this DbContext Azure KeyVault for DBContext:“尚未为此DbContext配置数据库提供程序” - Azure KeyVault for DBContext: “No database provider has been configured for this DbContext” InvalidOperationException:没有为此DbContext配置数据库提供程序。 可以通过覆盖DbContext来配置提供程序 - InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext 没有为此 DbContext 配置数据库提供程序。 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序 - No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring ASP.NET API:尚未为此DbContext配置任何数据库提供程序 - ASP.NET API: No database provider has been configured for this DbContext add-Migration 错误 没有为此 DbContext 配置数据库提供程序 - add-Migration Error No database provider has been configured for this DbContext 添加迁移错误:尚未为此DbContext配置数据库提供程序 - Add migration error: No database provider has been configured for this DbContext LINQPad EF Core 没有为此 DbContext 配置数据库提供程序 - LINQPad EF Core no database provider has been configured for this DbContext InvalidOperationException:没有为此 DbContext 配置具有存储库模式的数据库提供程序 - InvalidOperationException: No database provider has been configured for this DbContext with Repository pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM