AddDbContext was called with configuration, but the context type 'NewsContext' only declares

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AddDbContext was called with configuration, but the context type 'NewsContext' only declares相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/cep' title='cep'>cep   called   鏂规硶   top   microsoft   image   娉ㄥ叆   ros   base   

闂

An error occurred while starting the application.

ArgumentException: AddDbContext was called with configuration, but the context type 鈥?/span>NewsContext鈥?/span> only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then 鈥?/span>NewsContext鈥?/span> 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 鈥?/span>NewsContext鈥?/span> only declares a parameterless constructor. This means that the configuration passed to AddDbContext will never be used. If configuration is passed to AddDbContext, then 鈥?/span>NewsContext鈥?/span> 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()

 

鎶€鏈垎浜浘鐗? src=

 

 

鍘熷洜

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锛岄偅涔堥渶瑕佹坊鍔犱竴涓狣bContextOptions<NewsContext>绫诲瀷鍙傛暟鐨勬瀯閫犲嚱鏁板埌NewsContext绫汇€傚惁鍒?net core 涓嶈兘娉ㄥ叆鏃跺甫涓夾ddDbContext娣诲姞鐨勯厤缃?/p>

 

瑙e喅鏂规硶

濡備笂闈㈡墍璇达紝NewsContext绫绘坊鍔犱竴涓狣bContextOptions<NewsContext>绫诲瀷鍙傛暟鐨勬瀯閫犲嚱鏁?/p>

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的主要内容,如果未能解决你的问题,请参考以下文章

OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called

Error: Exception was raised when calling event-notify

Android - A WebView method was called on thread 'JavaBridge

webView.loadUrl 错误:A WebView method was called on thread 'JavaBridge'.

Flutter笔记-引入CupertinoAlertDialog报‘The getter alertDialogLabel was called on null’问题

Flutter笔记-引入CupertinoAlertDialog报‘The getter alertDialogLabel was called on null’问题