gpt4 book ai didi

c# - 尚未为此 DbContext 配置数据库提供程序以获取 Asp.net Core 中的方法

转载 作者:行者123 更新时间:2023-11-30 23:17:28 24 4
gpt4 key购买 nike

我通过 Asp.net 核心创建了一个 web api 项目,并向其中添加了一个 api Controller (名称为 BlogController),在博客 Controller 中我有一个 get 方法 GetAllBlog 这是我的 Controller :

[Route("api/[controller]")]
public class BlogController : Controller
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public IContext _context { get; set; }
public BlogController(IContext ctx)
{
_context = ctx;
}

[HttpGet]
public IEnumerable<Blog> GetAllBlog()
{
return _context.Blogs.ToList();
}
}

这是我的 IContext 和模型:

public interface IContext : IDisposable
{
DbSet<Blog> Blogs { get; set; }
DbSet<Post> Posts { get; set; }
int SaveChanges();
}

和上下文:

public class Context : DbContext, IContext
{
public Context(DbContextOptions<Context> options) : base(options)
{ }
public virtual DbSet<Blog> Blogs { get; set; }
public virtual DbSet<Post> Posts { get; set; }
}

和模型:

public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public DateTime? CreationDate { get; set; }
public virtual IList<Post> Posts { get; set; }
}

当我调用 GetAllBlog() 时,我得到了这个错误:

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. what is the problem?

更新:这是 Startup 类中的 configurationservice 方法:

public void ConfigureServices(IServiceCollection services)
{
var connection = @"Data Source=.;Initial Catalog=RestfullServices;Integrated Security=true";
services.AddDbContext<Context>(options => options.UseSqlServer(connection));
services.AddScoped<IContext>(p => new Context(new DbContextOptions<Context>()));
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
}

最佳答案

配置DbContext时

services.AddDbContext<Context>(options => options.UseSqlServer(connection));

您将其配置为使用特定选项 options.UseSqlServer(connection)

但是在配置作用域上下文抽象时

services.AddScoped<IContext>(p => new Context(new DbContextOptions<Context>()));

正在创建一个新的 Context,其配置与之前的配置完全不同。

通过像这样在启动期间更改 IContext 向 DI 框架注册的方式

services.AddScoped<IContext, Context>();

当创建 Context 实例时,DI 框架将使用 AddDbContext 配置,在创建 DbContext 实例时,它将具有您希望从启动配置中使用的选项.

Startup.ConfigurServices 最终看起来像这样...

public void ConfigureServices(IServiceCollection services) {
var connection = @"Data Source=.;Initial Catalog=RestfullServices;Integrated Security=true";
services.AddDbContext<Context>(options => options.UseSqlServer(connection));
services.AddScoped<IContext, Context>();
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
}

关于c# - 尚未为此 DbContext 配置数据库提供程序以获取 Asp.net Core 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com