[英]EntityFramework Core Database.EnsureCreated doesn't create database
I create a project with dotnetcore and entity framework core. 我用dotnetcore和实体框架核心创建一个项目。 I use MySql database and I added dependency to
SapientGuardian.EntityFrameworkCore.MySql
version 7.1.19
. 我使用MySQL数据库和我说依赖性
SapientGuardian.EntityFrameworkCore.MySql
版本7.1.19
。
I created a SeedData
class where initialize my database: 我创建了一个
SeedData
类,用于初始化数据库:
public class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (MyDbContext context = new MyDbContext(serviceProvider.GetRequiredService<DbContextOptions<MyDbContext>>())) {
context.Database.EnsureCreated();
}
}
}
The call to seed class: 种子类的调用:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Other code
SeedData.Initialize(app.ApplicationServices);
}
And the configuration of service: 并配置服务:
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>>();
}
When I start the project I get an exception on EnsureCreate
call. 当我启动项目时,在
EnsureCreate
调用上会出现异常。 Exception message say Authentication failed
but if I manually create database (without tables) my connection string works fine and EnsureCreated
simply create tables for my entities. 异常消息说
Authentication failed
但是如果我手动创建数据库(没有表),我的连接字符串可以正常工作,而EnsureCreated
创建的只是为我的实体创建表。
What's wrong? 怎么了? It's a problem with
SapientGuardian.EntityFrameworkCore.MySql
? SapientGuardian.EntityFrameworkCore.MySql
有问题吗?
I had the same problem you described. 我遇到了您描述的相同问题。 It looks like the SapientGuardian provider might simply have an incomplete implementation in this version.
看起来SapientGuardian提供程序可能只是在此版本中实现不完整。
The SapientGuardian project's readme file actually suggests using a more actively maintained provider such as Pomelo.EntityFrameworkCore.MySql . SapientGuardian项目的自述文件实际上建议使用维护程度更高的提供程序,例如Pomelo.EntityFrameworkCore.MySql 。 So I switched my project to Pomelo.
因此我将项目切换到Pomelo。 Now the call to EnsureCreated creates the database as expected and does not have an authentication failed exception.
现在,对确保创建的调用将按预期方式创建数据库,并且没有身份验证失败异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.