AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?
问题
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; }
}
}
AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?的更多相关文章
- AddDbContext was called with configuration, but the context type 'MyDBContext' only declares a parameterless constructor
System.ArgumentException HResult=0x80070057 Message=AddDbContext was called with configuration, but ...
- Code First 启用迁移时出错 "No context type was found in the assembly"
问题:Code First 启用迁移时找不到上下文DbContext所在的项目. PM> Enable-Migrations No context type was found in the a ...
- VS EF Error: Configuration Error extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"
错误截图: Configuration Error :<add extension=".edmx" type="System.Data.Entity.Design. ...
- EntityFramework 启用迁移 Enable-Migrations 报异常 "No context type was found in the assembly"
转自:http://www.cnblogs.com/stevenhqq/archive/2013/04/18/3028350.html 以前做项目的时候,没有采用分类库的形式,所以迁移一致非常顺利,没 ...
- EntityFramework codefirst Enable-Migrations No context type was found in the assembly “MyApp.Web” error
Enable-Migrations -Force -ContextTypeName "MyApp.Repository.DataContext" -ProjectName &quo ...
- No context type was found in the assembly
如果解决方法中有多个项目存在,记住要在默认项目中选择你需要的项目进行 enable-migrations add-migration 以及updatebase
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8201936 在前文中,我们简要介绍了Andro ...
- The logback manual #03# Configuration
索引 Configuration in logback Automatically configuring logback Automatic configuration with logback-t ...
随机推荐
- python3 安装虚拟镜像
virtualenvwrapper--提供了一系列命令使得和虚拟环境工作变得愉快很多,他把你所需要的虚拟环境都放在一个地方. 1.先安装virtualenv:pip install virtualen ...
- Maven项目编译版本的问题和Spring中没有导入核心包
idea中maven项目的编译: 方案1:maven的settings.xml中指定全局默认编译版本 <profile> <id>jdk-1.8</id> < ...
- Java工程打包成jar可执行文件
将一个工程中的类打包成jar文件,步骤参考如下: 1.选择file -> project structure 2. 选择Arifacts->JAR->form modules wit ...
- CC02:原串翻转
题目 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniString,请返回一个string,为翻转后的字符串.保证字 ...
- 更新常用的js工具函数
在手机调试时打印代码<script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></ ...
- Netty(1-1)Discard
一.DiscardServerHandler import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext ...
- Net Core2.0下使用Dapper
Net Core2.0下使用Dapper 今天成功把.Net Framework下使用Dapper进行封装的ORM成功迁移到.Net Core 2.0上,在迁移的过程中也遇到一些很有意思的问题,值得和 ...
- (转)sudo配置文件/etc/sudoers详解及实战用法
sudo配置文件/etc/sudoers详解及实战用法 原文:http://blog.csdn.net/field_yang/article/details/51547804 一.sudo执行命令的流 ...
- SQL server 游标,随机数使用
SELECT * FROM [goods] DECLARE test_cursor CURSOR scroll FOR SELECT goods_no, [unitprice] FROM [goods ...
- Zookeeper+websocket实现对分布式服务器的实时监控(附源码下载)
我就是个封面 Zookeeper简介 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统. 简单来说就是一个Zookeeper注册同步中心,内部结构为一个树形目录,每个节点上 ...