将“IDesignTimeDbContextFactory<ApplicationDbContext>”的实现添加到项目中

Ham*_*mid 1 asp.net asp.net-mvc entity-framework-core asp.net-core-mvc asp.net-core

我创建了一个 asp.net core 项目,并选择 UseIndiviualUserAccount 作为我的身份验证类型,在通过 Visual Studio 准备项目后,我得到了一些预构建的类和控制器,显然我得到了迁移文件夹及其配置,因为我不想使用 asp.net 的 defulat 结构core,我删除了迁移文件夹,创建了一个新的类库,并将其重命名为 MyProject.Core。在 MyProject.Core 类库中,我创建了我的数据库模型和 dbContext 类,因此我需要运行 add-migration“init”命令来创建我的数据库,所以我这样做了,但出现了以下错误,注意:我正在运行添加迁移命令在 PMC 中的 Myproject.Core 中! 无法创建“ApplicationDbContext”类型的对象。将“IDesignTimeDbContextFactory”的实现添加到项目中,或参阅https://go.microsoft.com/fwlink/?linkid=851728了解设计时支持的其他模式。

我该如何修复此错误,我在当前网站和其他网站上阅读了一些文章,但无法解决我的问题。

我的项目结构

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

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}


public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<IUnitOfWork, UnitOfWork>();
        services.AddTransient<IProvinceRepository, ProvinceRepository>();
        services.AddTransient<IBrandRepository, BrandRepository>();

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Add application services.
        services.AddTransient<IEmailSender, EmailSender>();

        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)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
    {
    }
    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        // Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }
Run Code Online (Sandbox Code Playgroud)

smo*_*nes 5

当 EF 不知道应用程序的入口点在哪里时,可能会发生这种情况。如果您使用的. 首先,确保您正在从包含迁移的项目运行迁移。IDesignTimeDbContextFactoryBuildWebHost

例如...如果您正在使用Package Manager Console

cd .\MyProject.Core
Run Code Online (Sandbox Code Playgroud)

然后使用运行迁移startup-project并将其指向带有 的项目BuildWebHost

dotnet ef --startup-project ../MyProject.Web/ migrations add Initial
Run Code Online (Sandbox Code Playgroud)

否则 EF 将不知道您的实现在哪里。