[英]InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext
I am working on project with no repository pattern. 我正在没有存储库模式的项目上工作。 I am trying to seperate my business logic from controllers, kind of setting up a business layer.
我试图将我的业务逻辑与控制器分开,这是建立业务层的一种方式。 I do the following to get all of my users.
我执行以下操作以获取所有用户。
DbContext 的DbContext
public class DatabaseContext : DbContext
{
public DatabaseContext() : base() { }
public DatabaseContext(DbContextOptions options) : base(options) { }
public DbSet<User> Users { get; set; }
public DbSet<OvertimeRequest> OvertimeRequests { get; set; }
public DbSet<HolidayRequest> HolidayRequests { get; set; }
public DbSet<PaymentRequest> PaymentRequests { get; set; }
}
OvertimeRequestBusiness OvertimeRequestBusiness
public class OvertimeRequestBusiness
{
public static OvertimeRequestBusiness Instance { get; } = new
OvertimeRequestBusiness();
public OvertimeRequestBusiness() { }
public async Task<List<User>> GetAllUsersAsync()
{
using (var ctx = new DatabaseContext())
{
var query = ctx.Users;
var res = await query.ToListAsync();
return res;
}
}
}
Controller 调节器
[Route("users"), HttpGet]
public async Task<List<User>> GetAllUsers()
{
return await OvertimeRequestBusiness.Instance.GetAllUsersAsync();
}
And the error I get is 我得到的错误是
InvalidOperationException: No database provider has been configured for this DbContext.
InvalidOperationException:没有为此DbContext配置数据库提供程序。 A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider.
可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。 If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext**
如果使用AddDbContext,则还要确保您的DbContext类型在其构造函数中接受DbContextOptions <TContext>对象,并将其传递给DbContext **的基本构造函数**
Without changing the OvertimeRequestBusiness
is to set db connection string in OnCongfiguring
method of DbContext
class : 无需更改
OvertimeRequestBusiness
即可在DbContext
类的OnCongfiguring
方法中设置数据库连接字符串:
Setting connect string in `appsettings.json : 在`appsettings.json'中设置连接字符串:
"ConnectionStrings": { "MyConnectionString": "Server=(localdb)\\\\mssqllocaldb;Database=RazorPagesMovieContext-1234;Trusted_Connection=True;MultipleActiveResultSets=true" },
Create Setting.cs
: 创建
Setting.cs
:
public class Setting { public static string ConnectionString { get; set; } }
Config the connectstring in Startup.cs
: 在
Startup.cs
配置Startup.cs
:
Setting.ConnectionString = Configuration.GetSection("ConnectionStrings:MyConnectionString").Value;
Modify the DatabaseContext
: 修改
DatabaseContext
:
public class DatabaseContext : DbContext { public DbSet<User> Users { get; set; } public DbSet<OvertimeRequest> OvertimeRequests { get; set; } public DbSet<HolidayRequest> HolidayRequests { get; set; } public DbSet<PaymentRequest> PaymentRequests { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(Setting.ConnectionString); } } }
But the common way is to use DbContext with dependency injection : 但是常见的方法是将DbContext与依赖项注入一起使用:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
Your DatabaseContext
constructor needs to point to the connection string. 您的
DatabaseContext
构造函数需要指向连接字符串。
Change it to be 更改为
public class DatabaseContext : DbContext
{
// Tell DbContext to look for the "MyConnectionString" in .config.
public DatabaseContext() : base("MyConnectionString") { }
public DatabaseContext(DbContextOptions options) : base(options) { }
public DbSet<User> Users { get; set; }
public DbSet<OvertimeRequest> OvertimeRequests { get; set; }
public DbSet<HolidayRequest> HolidayRequests { get; set; }
public DbSet<PaymentRequest> PaymentRequests { get; set; }
}
Where your connection string is in web.config 连接字符串在web.config中的位置
<configuration>
<connectionStrings>
<add name="MyConnectionString" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Initial Catalog=ABC;Integrated Security=True;MultipleActiveResultSets=true"></add>
</connectionStrings>
</configuration>
I deleted the default parameterless constructor of DbContext and then passed the DbContextBuilderOption as parameter in OvertimeRequestBusiness. 我删除了DbContext的默认无参数构造函数,然后将DbContextBuilderOption作为参数传递给OvertimeRequestBusiness。 This did the trick for me.
这帮了我大忙。
OvertimeRequestBusiness Looks like this now OvertimeRequestBusiness现在看起来像这样
public class OvertimeRequestBusiness
{
public static OvertimeRequestBusiness Instance { get; } = new OvertimeRequestBusiness();
DbContextOptionsBuilder<DatabaseContext> _optionsBuilder;
public OvertimeRequestBusiness() {
var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
optionsBuilder.UseSqlServer(@"ConnectionString");
_optionsBuilder = optionsBuilder;
}
public async Task<List<User>> GetAllUsersAsync()
{
using (var ctx = new DatabaseContext(_optionsBuilder.Options))
{
var query = ctx.Users;
var res = await query.ToListAsync();
return res;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.