AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?
2024-09-07 20:25:35
问题
An error occurred while starting the application. ArgumentException: AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then 'NewsContext' should declare a constructor that accepts a DbContextOptions<NewsContext> and must pass it to the base constructor for DbContext.
Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.CheckContextConstructors<TContext>() ArgumentException: AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then 'NewsContext' should declare a constructor that accepts a DbContextOptions<NewsContext> and must pass it to the base constructor for DbContext.
Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.CheckContextConstructors<TContext>()
Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext<TContextService, TContextImplementation>(IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext<TContextService, TContextImplementation>(IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext<TContext>(IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime)
News.Startup.ConfigureServices(IServiceCollection services) in Startup.cs
+
services.AddDbContext<NewsContext>(options =>
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
原因
NewsContext.cs
using Microsoft.EntityFrameworkCore; namespace News.Service
{
public class NewsContext : DbContext
{
public DbSet<News.Model.Entity.News> News { get; set; }
public DbSet<News.Model.Entity.Banner> Banner { get; set; }
public DbSet<News.Model.Entity.Comment> Comment { get; set; }
public DbSet<News.Model.Entity.NewsClassify> NewsClassify { get; set; }
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...... services.AddDbContext<NewsContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("MsSqlConnection"), db => db.UseRowNumberForPaging());
}); ...... }
该错误表示,如果通过AddDbContext配置NewsContext,那么需要添加一个DbContextOptions<NewsContext>类型参数的构造函数到NewsContext类。否则.net core 不能注入时带上AddDbContext添加的配置
解决方法
如上面所说,NewsContext类添加一个DbContextOptions<NewsContext>类型参数的构造函数
using Microsoft.EntityFrameworkCore; namespace News.Service
{
public class NewsContext : DbContext
{
public NewsContext(DbContextOptions<NewsContext> options) : base(options)
{
} public DbSet<News.Model.Entity.News> News { get; set; }
public DbSet<News.Model.Entity.Banner> Banner { get; set; }
public DbSet<News.Model.Entity.Comment> Comment { get; set; }
public DbSet<News.Model.Entity.NewsClassify> NewsClassify { get; set; }
}
}
最新文章
- STMFD 和LDMFD指令
- 【转】JavaScript获取节点类型、节点名称和节点值
- 前端mvc框架backbone.js入门[转]
- js 模块开发之一(模块开发价值)
- sublime3笔记
- Chrome浏览器允许跨域请求配置
- VM VirtualBox安装Centos6.5
- SElinux用户管理操作
- mongodb 定时备份
- 【Git】CentOS7 通过源码安装Git
- 朱晔和你聊Spring系列S1E3:Spring咖啡罐里的豆子
- ACM-ICPC 2018 焦作赛区网络预赛 L Poor God Water(矩阵快速幂,BM)
- Software tips
- DNS服务器能遭受到的DDNS攻击类型
- noip模拟【service】
- 【微信公众号开发】【8】网页授权获取用户基本信息(OAuth 2.0)
- Arduino入门笔记(3):单LED闪烁
- php学习目录
- MT【147】又见最大最小
- sql分组排序取top
热门文章
- Delphi调用C# 编写dll动态库
- CC06:像素翻转
- Django之Form自定义验证规则
- Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers
- Vijos 1002 过河 dp + 思维
- Funsioncharts 线图 破解
- webpack.config.js====output出口文件的配置
- Java Lambda表达式教程与示例
- 在docker上centos7 编译安装php7
- 【转】HTTPS系列干货(一):HTTPS 原理详解