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?

原文地址:https://www.cnblogs.com/Zev_Fung/p/10089974.html

时间: 2024-08-04 21:16:13

AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?的相关文章

No context type was found in the assembly &#39;xxx.xxxx&#39;. CodeFirst Ef错误

最简单的解决方案是将启动项目设置为你要生产Migration的项目. 例如: 我这边将Try.EntityFramework设置为启动项目.并且准备在该项目下生成Migration文件.这里的默认项目同样设置为Try.EntityFramework.就OK了. 2.另一种方案. 查看get-help Enable-Migrations帮助,发现启用迁移命令带了几个参数. Enable-Migrations [-ContextTypeName <String>] [-EnableAutomati

uwp - 解决在使用EntityFrameworkCore 指令“add-migration xx” 时 报错“No context type was found in the assembly/no migrations configuration type was found in the assembly”

报错截图如下: 网上说的办法试过后不行,然后我卸载所有nuget包从头一步步来,终于找到了问题,在到"Add-Migration"这一步,别急执行指令,先重启一下VS,然后再执行指令就可以了,重启前可以重新生成一下项目. 也就是说在创建完数据模型.配置文件app.config等一些列步骤后先重启VS.

EntityFramework codefirst Enable-Migrations No context type was found in the assembly &ldquo;MyApp.Web&rdquo; error

Enable-Migrations -Force -ContextTypeName "MyApp.Repository.DataContext" -ProjectName "MyApp.Repository" -Startup "MyApp.Web" -ConnectionStringName "DataContext" -verbose

EntityFramework 启用迁移 Enable-Migrations 报异常 &quot;No context type was found in the assembly&quot;

转自:http://www.cnblogs.com/stevenhqq/archive/2013/04/18/3028350.html 以前做项目的时候,没有采用分类库的形式,所以迁移一致非常顺利,没有出现过任何状况. 这次做项目稍微有点大,必须要分类库才方便开发维护. 在解决方案中启用项目EntityFramework迁移时却发生了异常. 异常说在我的项目中没有找到DBContext类. 这个DBContext类确实没有放在启动项目下面,是另外建立了一个独立的类库来存放. 在启动项目中引用了却

keil 的 配置向导 configuration wizard(转载)

keil 的 配置向导 configuration wizard 以前发现keil 的很棒的功能 今天终于会用了.  分享给大家.转载的. 一 前言          很多人使用keil的时候感觉keil的configuration wizard 很神奇,用起来特别方便,但是苦于不知道怎么去编写自己的configuration wizard,其实keil的help文档就有,只是很多人用着感觉英文不方便,又或者看了没理解,为此,特写了一个教程,希望大家能从中学到一些知识. 二 基本介绍 Confi

Entity Framework 6.0 Tutorials(3):Code-based Configuration

Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can configure Entity Framework related settings using the code which had been previously configured in the <entityframework> section of the app.config. Ho

Spring.NET 配置错误【Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常。】

用VS 2013 ,配置Spring后,运行时出现如下错误: Spring.Context.Support.ContextRegistry”的类型初始值设定项引发异常. 原因:App.config配置文件,startup放到文件前了. 解决方法:将startup放到最后,问题没有了. <?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <

Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析

文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8201936 在前文中,我们简要介绍了Android应用程序窗口的框架.Android应用程序窗口在运行的过程中,需要访问一些特定的资源或者类.这些特定的资源或者类构成了Android应用程序的运行上下文环境,Android应用程序窗口可以通过一个Context接口来访问它,这个Context接口也是我们在开发应用程序时经常碰到的.在本文中,我们

阅读Logback文档笔记--Logback的Configuration配置

要在项目中引入日志,需要一定的代价,据统计日志代码量将会占整个代码量的4%左右.因此我们需要一个工具管理这些日志声明. 对于Log4j.properties可以通过以下网址转化成logback.xml http://logback.qos.ch/translator/ Logback如何查找配置文件: 在classpath中查找logback.groovy 如果不存在,则在classpath中查找logback-test.xml 如果不存在,继续查找logback.xml 如果不存在,则由jdk