昨天我开始学习 ASP.NET 5 MVC 6 with Entity Framework 7.0.0-rc1-final
并按照本教程进行操作:Link 。问题是当我运行它时,它会在此函数上抛出 NullPointerException:
if (serviceProvider.GetService<IRelationalDatabaseCreator>().Exists())
{//adding some entities }
我的连接字符串是:"Server=(localdb)\\MSSQLLocalDB; Database=Test; Trusted_Connection=True; MultipleActiveResultSets=true"
在 Startup.cs 中我有以下代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<Models.HRPContext>(options =>
{
options.UseSqlServer(Configuration["Data:ConnectionString"]);
});
.......
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
Utils.AddSampleData.Initialize(app.ApplicationServices);
......
}
数据库 Test 尚未创建。有人解决了这个异常吗?谢谢
最佳答案
该教程是 out of date 。使用this one相反。
在更新的教程中,if 语句不存在。相反,它通过检查上下文对象的 Database
属性来检查数据库是否存在:
if (context.Database == null)
{
throw new Exception("DB is null");
}
关于c# - IRelationalDatabaseCreator 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34675016/