简体   繁体   English

向项目添加“IDesignTimeDbContextFactory”的实现?

[英]Add an implementation of 'IDesignTimeDbContextFactory' to the project?

Currently following a course from early 2018.目前正在学习 2018 年初的课程。

After running Add-Migration Initial in the Package Manager Console在包管理器控制台中运行Add-Migration Initial

This is my error message ;这是我的错误信息;

Unable to create an object of type 'AppDbContext'.无法创建“AppDbContext”类型的对象。 Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.将“IDesignTimeDbContextFactory”的实现添加到项目中,或参阅https://go.microsoft.com/fwlink/?linkid=851728了解设计时支持的其他模式。

The link says to add...链接说要添加...

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace MyProject
{
    public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
    {
        public BloggingContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
            optionsBuilder.UseSqlite("Data Source=blog.db");

            return new BloggingContext(optionsBuilder.Options);
        }
    }
}

to my startup.cs...到我的startup.cs ...

This is my startup class这是我的创业班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using BethanysPieShop.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using WebApplication5.Models;

namespace BethanysPieShop
{
    public class Startup
    {
        private IConfigurationRoot _configurationRoot;

        public Startup(IHostingEnvironment hostingEnvironment)
        {
            _configurationRoot = new ConfigurationBuilder()
                .SetBasePath(hostingEnvironment.ContentRootPath)
                .AddJsonFile("appsettings.json")
                .Build();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<AppDbContext>(options => 
                            options.UseSqlServer(_configurationRoot.GetConnectionString("DefaultConnection")));

            services.AddTransient<ICategoryRepository, CategoryRepository>();
            services.AddTransient<IPieRepository, PieRepository>();

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseDeveloperExceptionPage();
            app.UseStatusCodePages();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }
    }
}

I'm using Entity Framework Core Tools 2.1.2我正在使用 Entity Framework Core Tools 2.1.2

AppDbContext.cs AppDbContext.cs

using BethanysPieShop.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication5.Models
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {
        }

        public DbSet<Category> Categories { get; set; }
        public DbSet<Pie> Pies { get; set; }

        public class DbSet
        {
        }
    }
}

Where/location do I implement their code in my code?我在哪里/位置在我的代码中实现他们的代码?

What variables do I change?我改变了哪些变量?

Try putting this:试着把这个:

services.AddScoped(typeof(IDesignTimeDbContextFactory<BloggingContext>), typeof(BloggingContextFactory));

In Startup.cs, method ConfigureServices, below the services.AddTransient < IPieRepository, PieRepository >();在 Startup.cs 中,方法 ConfigureServices,在 services.AddTransient < IPieRepository, PieRepository >() 之下;

I hope you find it useful希望对你有帮助

Last I remember this issue was caused because you're not using the proper WebHostBuilder Method name see this github issue最后我记得这个问题是因为您没有使用正确的 WebHostBuilder 方法名称引起的,请参阅此github 问题

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    //.Net-core relies on Duck Typing during migrations and scaffolding
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法创建类型为“ApplicationDbContext”的对象。 添加 &#39;IDesignTimeDbContextFactory 的实现<ApplicationDbContext> &#39; 到项目 - Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project 添加&#39;IDesignTimeDbContextFactory的实现 <ServicesDbContext> 在.NET Core 2.0中创建迁移步骤时遇到项目错误 - Add an implementation of 'IDesignTimeDbContextFactory<ServicesDbContext>' to the project error while creating a migration step in .NET core 2.0 如何添加“IDesignTimeDbContextFactory”的实现<DataContext> &#39; 到 asp.net-core-2.0 中的项目 - How to Add an implementation of 'IDesignTimeDbContextFactory<DataContext>' to the project in asp.net-core-2.0 EF Core 2.1-无法创建“ MyAppContext”类型的对象。 添加&#39;IDesignTimeDbContextFactory的实现 <MyAppContext> &#39;到该项目, - EF Core 2.1 - Unable to create an object of type 'MyAppContext'. Add an implementation of 'IDesignTimeDbContextFactory<MyAppContext>' to the project, 尝试将字符串列表添加到代码第一个实体框架类中,现在它确实需要IDesignTimeDbContextFactory实现 - Tried adding a list of strings to code first entity framework class, now it really wants a IDesignTimeDbContextFactory implementation IDesignTimeDbContextFactory并不总是需要? - IDesignTimeDbContextFactory not always needed? IDesignTimeDbContextFactory[TContext] 违反了 - IDesignTimeDbContextFactory[TContext] violates the 接口实施到我的项目中 - Interface implementation in to my project 如何在运行时更改连接字符串 IDesignTimeDbContextFactory - How to change connection string IDesignTimeDbContextFactory in run time 添加属性的默认实现 - Add a default implementation of properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM