zoukankan      html  css  js  c++  java
  • 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; }
        }
    }
  • 相关阅读:
    scp命令报错-bash: scp: command not found
    shell比较两个字符串是否相等
    bat脚本:自动压缩n天前的文件【转载】
    shell bash判断文件或文件夹是否存在
    linux文件分割(将大的日志文件分割成小的)【转载】
    TCP/IP模型各个层次的功能和协议
    nginx初级安装配置
    Heartbeat+DRBD+MySQL高可用方案【转】
    【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
    oracle的exp和imp命令的使用【转载】
  • 原文地址:https://www.cnblogs.com/Zev_Fung/p/10089974.html
Copyright © 2011-2022 走看看