将“ IDesignTimeDbContextFactory”的实现添加到项目中?

时间:2019-01-24 17:55:31

标签: c# .net

目前正在学习从2018年初开始的课程。

在Package Manager控制台中运行 Add-Migration Initial

这是我的错误消息;

  

无法创建“ AppDbContext”类型的对象。在项目中添加“ IDesignTimeDbContextFactory”的实现,或在设计时支持https://go.microsoft.com/fwlink/?linkid=851728的其他模式。

该链接显示要添加...

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);
        }
    }
}

到我的startup.cs ...

这是我的创业班

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();
        }
    }
}

我正在使用Entity Framework Core Tools 2.1.2

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
        {
        }
    }
}

我在哪里/位置在我的代码中实现他们的代码?

我要更改哪些变量?

2 个答案:

答案 0 :(得分:0)

尝试将其放置:

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

在Startup.cs中,服务下方的方法ConfigureServices.AddTransient ();

我希望您觉得它有用

答案 1 :(得分:0)

最后我记得这个问题是由于您没有使用正确的WebHostBuilder方法名称引起的,请参见此github issue

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();
}