[英]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.