[英]'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.