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 ...
随机推荐
- HDMI和VGA接口
HDMI英文全称为:High Definition Multimedia Interface,它是一种全数字化视频和声音发送接口,可以发送未压缩的音频及视频信号. HDMI接口与VGA接口区别如下: ...
- Python内建函数二
内置函数二: 1.lambda (匿名函数) 为了解决一些简答的需求而设计的一句话函数.不需要def来声明. def func(n): return n*n f = lambda n: n*n 注意: ...
- element-ui关于form表单在dialog中的重置
form表单的重置是以第一次打开的数据作为重置标准,如果先打开的是更新,那么重置之后以第一次更新的数据作为标准; Dialog 中的内容是懒加载的,目前 edit (更新)方法的写法导致 Form 刚 ...
- python实现批量远程执行命令及批量上传下载文件
#!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...
- log4j.properties错误及配置详解
当在Eclipse上运行MapReduce程序遇到以上问题时,请检查项目中是否有log4j.properties配置文件,或者配置文件是否正确. 刚接触Hadoop的时候不太了解log4j.prope ...
- Aspose.word组件介绍
阅读目录 1.基本介绍 2.文档对象模型概述 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html 本博客其他.NET开 ...
- Python使用selenium进行爬虫(一)
JAVA爬虫框架很多,类似JSOUP,WEBLOGIC之类的爬虫框架都十分好用,个人认为爬虫的大致思路就是: 1.挑选需求爬的URL地址,将其放入需求网络爬虫的队列,也可以把爬到的符合一定需求的地址放 ...
- 常用API(Object、String、StringBuffer、用户登陆注册)
常用API 今日内容介绍 u Object u String u StringBuilder 第1章 Java的API及Object类 在以前的学习过程中,我们都在学习对象基本特征.对象的使用以及对象 ...
- vue2.0:(八)、外卖App弹窗部分知识点总结
本篇文章是对外卖App弹窗部分知识点的总结. 知识点一:如何从接口取出不同的图片. 答: 1.header.vue: 代码: <ul v-if="seller.supports&quo ...
- 零基础逆向工程23_PE结构07_重定位表_IAT表(待补充)
重定位表 待补充 IAT表 待补充