我使用dotnetcore和实体框架核心创建一个项目。我使用MySql数据库,并且我添加了对SapientGuardian.EntityFrameworkCore.MySql
版本7.1.19
的依赖。EntityFramework核心数据库.EnsureCreated不创建数据库
我创建了一个SeedData
类,其中初始化我的数据库:
public class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (MyDbContext context = new MyDbContext(serviceProvider.GetRequiredService<DbContextOptions<MyDbContext>>())) {
context.Database.EnsureCreated();
}
}
}
种子类召唤:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Other code
SeedData.Initialize(app.ApplicationServices);
}
和服务的配置:
public void ConfigureServices(IServiceCollection services)
{
// Other code
// Add database
services.AddDbContext<MyDbContext>(options => {
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"));
});
services.AddScoped<Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptions, Microsoft.EntityFrameworkCore.DbContextOptions<MyDbContext>>();
}
当我开始该项目我在EnsureCreate
电话上遇到异常。异常消息说Authentication failed
,但如果我手动创建数据库(没有表),我的连接字符串工作正常,EnsureCreated
只为我的实体创建表。
怎么了?这是SapientGuardian.EntityFrameworkCore.MySql
的问题?
erikscandola,你解决了吗? –
@JohnSmith,还不是 – erikscandola
@erikscandola你有一个空的数据库是否存在? – bravohex