相关疑难解决方法(0)

升级到 Core 2 Preview 2 后出现“无法解析 .. 类型的服务”

我刚刚升级到 ASP.NET Core 2 Preview 2 并遇到了依赖注入的问题。我得到

运行项目时,无法解析类型为“LC.Tools.API.Startup”的方法“配置”的参数“上下文”的“LC.Tools.API.Data.GenericDbContext”类型的服务。

我用旧版本的时候没有这个问题。

数据库上下文(通用数据库上下文):

namespace LC.Tools.API.Data
{
    public class GenericDbContext : DbContext
    {
        public GenericDbContext(DbContextOptions<GenericDbContext> options) : base(options) { }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            //Generic
            builder.Entity<Client>();
            builder.Entity<Graphic>();
            .
            .
            .
            .
            .

            //Shop
            builder.Entity<Models.Shop.Store>().ToTable("ShopClient");
            builder.Entity<Models.Shop.Category>().ToTable("ShopCategory");
            .
            .
            .
            .
            .
            .
    }
}
Run Code Online (Sandbox Code Playgroud)

启动.cs:

namespace LC.Tools.API
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework asp.net-core asp.net-core-2.0

3
推荐指数
1
解决办法
8019
查看次数