AddDbContext was called with configuration, but the context type 'NewsContext' only declares a parameterless constructor?
阿新 • • 發佈:2018-12-09
問題
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 baseconstructor 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; } } }