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 ...
随机推荐
- Unity T4M 中文讲解
http://blog.csdn.net/tianmao111/article/details/46482963 现在在u3d圈里流行了一种地形转换器(或者叫编辑器吧),但是经查阅之后,似乎还没有中文 ...
- 图论2 最近公共祖先LCA
模板 吸取洛谷P3379的教训,我决定换板子(其实本质都是倍增是一样的),把vector换成了边表 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下 ...
- Glassfish 设置时区
对于Glassfish domain 或者instance下,某个日志的时区不对,前提是系统时区争取. 可以尝试通过如下命令查看jvm 时区设置 asadmin list-jvm-options 如果 ...
- CentOS与Ubuntu修改主机名
CentOS 1.执行hostname查看主机名 2.hostname + 主机名 使需要修改的主机名立即生效,但是下次重启会失效,故需要执行第三步 3.vim /etc/sysconfig/net ...
- Codeforces 1141F2(贪心、预处理)
要点 一开始dp然后码力太辣鸡并且算法带假于是调了很久一交还WA在28-- 吐槽完毕.后来想拿栈优化dp时发现其实完全不需要dp,贪心选取即可,当前的不兼容就干脆不要它了,结果不会变差.然后想要什么就 ...
- UVa1471
保留有价值的数字的做法,实际上这道题因为n只有1e5,所以不需要这种优化. #include<bits/stdc++.h> #define inf 0x3f3f3f3f ; using n ...
- NET Core 2.0 的 REST API
用ASP.NET Core 2.0 建立规范的 REST API -- 预备知识 (2) + 准备项目 上一部分预备知识在这 http://www.cnblogs.com/cgzl/p/9010978 ...
- 《java学习三》并发编程 -------线程池原理剖析
阻塞队列与非阻塞队 阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞.试图从空的阻塞队列中获取元素的线程将会被阻塞,直到 ...
- App配置页面头部
记录一下 App配置页面头部 例 上图红色框部分就是自己配置的头部 //我的客户 "/OACustomer/Index": { title: "我的客户", h ...
- c#基础 base和this的区别,在继承上面
base public Person(string name, int age, char gender) { this.Name = name; this.Age = age; this.Gende ...