【问题标题】:Error saying.No database provider has been configured for this DbContext错误提示。没有为此 DbContext 配置数据库提供程序
【发布时间】:2021-05-04 18:11:06
【问题描述】:

我在访问数据库表中的数据时遇到错误,说 InvalidOperationException: 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<TContext> object in its constructor and passes it to the base constructor for DbContext.

我使用了通用存储库模式。我在单独的类库项目中有我的 DbContext,即 BlazorContext 和模型类。

任何帮助都将不胜感激。

下面是我在 appsettings.json 中的连接字符串

 "ConnectionStrings": {
"myconn": "server=DESKTOP-VM2VP34; database=BlazorDB;Trusted_Connection=True;"
  },<br><br>

下面是我的startup.cs

services.AddDbContext<BlazorContext>(item => item.UseSqlServer(Configuration.GetConnectionString("myconn")));

下面是我的 DbContext,即 BlazorContext

namespace Blazor.Model.Models
{
public class BlazorContext:DbContext
{
    public BlazorContext()
    {
    }

    public BlazorContext(DbContextOptions<BlazorContext> options)
        : base(options)
    {
        Database.EnsureCreated();
    }

    public DbSet<Person> Persons { get; set; }
}
}



下面是我的通用存储库实现,它显示错误

namespace Blazor.Repository.Implementation
{
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
    protected BlazorContext _entities;
    protected readonly DbSet<T> _dbset; // error in this line

    public GenericRepository(BlazorContext context)
    {
        _entities = context;
        _dbset = context.Set<T>();
    }

    public IEnumerable<T> GetAll()
    {
        return _dbset.AsEnumerable();
    }
}
}



下面是我的 GenericUnitOfWork

namespace Blazor.Repository.Implementation
{
public sealed class GenericUnitOfWork : IGenericUnitOfWork, IDisposable
{
    private BlazorContext entities = null;
    public GenericUnitOfWork()
    {
        entities = new BlazorContext();
    }

    public Dictionary<Type, object> repositories = new Dictionary<Type, object>();

    public IGenericRepository<T> Repository<T>() where T : class
    {
        if (repositories.Keys.Contains(typeof(T)) == true)
        {
            return repositories[typeof(T)] as IGenericRepository<T>;
        }
        var t = typeof(T);

        IGenericRepository<T> repo = new GenericRepository<T>(entities);//error in this line
        repositories.Add(typeof(T), repo);
        return repo;
    }
}
}

【问题讨论】:

  • 您的工作单元使用无参数构造函数手动创建数据库上下文。这是不正确的,因为您不会获得启动配置的上下文和非常糟糕的一般做法。你的工作单元应该有它的上下文注入。然后将工作单元注册为服务
  • @AluanHaddad : 你能帮我写代码吗
  • 我可以,但我建议你放弃整个 DAO 模式。 DbSet&lt;T&gt; 已经是一个通用存储库 - 包装它会适得其反,尴尬且不必要。同样,BlazorContext 作为DbContext 的子类型,已经是一个工作单元服务。直接使用这些类型,特别是如果您是框架新手
  • 我知道这是旧的,但我遇到了同样的问题。我的数据访问存储库项目与我的服务项目是分开的。服务项目是一个控制台应用程序。当我使用针对控制台应用程序的 -startupproject 运行添加迁移时,它不起作用。当我针对具有相同启动代码的 Web 应用程序时,它起作用了。我想知道这是否与 EF Core 工具检查启动项目的方式有关。

标签: c# asp.net-core entity-framework-core


【解决方案1】:

您的连接字符串模板不适用于 MS SQL 服务器。尝试使用这样的东西:

"Data Source=localhost;Initial Catalog=BlazorDB;Integrated Security=SSPI;Persist Security Info=True;"

如果它不起作用,那么您使用的不是 MS SQL 服务器,因此在您的启动中,您必须将“item => item.UseSqlServer”替换为其他提供程序。

【讨论】:

    猜你喜欢
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2017-09-02
    • 2019-02-17
    • 2019-10-12
    • 2016-11-15
    • 2018-11-12
    相关资源
    最近更新 更多