首先,我试图在我的数据库中植入样本数据.我已经读到这是做这件事的方法(在Startup.Configure)(请看ASP.NET Core RC2 Seed Database)
我在用ASP.NET Core 2.0和默认选项.
和往常一样,我在ConfigureServices
中登记了我的DbContext
分.
系统InvalidOperationException:'无法解析作用域服务
我的创业课程是这样的:
public abstract class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SGDTPContext>(options => options.UseInMemoryDatabase("MyDatabase"))
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
SeedDatabase(app);
}
private static void SeedDatabase(IApplicationBuilder app)
{
using (var context = app.ApplicationServices.GetRequiredService<SGDTPContext>())
{
// Seed the Database
//...
}
}
}
我做错了什么?