Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework Core Database.EnsureCreated doesn't create database

I create a project with dotnetcore and entity framework core. I use MySql database and I added dependency to SapientGuardian.EntityFrameworkCore.MySql version 7.1.19.

I created a SeedData class where initialize my database:

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. 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.

What's wrong? It's a problem with SapientGuardian.EntityFrameworkCore.MySql?

like image 579
erikscandola Avatar asked Feb 23 '17 10:02

erikscandola


People also ask

Does EF core create database?

At this point you can have EF create your database and create your schema from the migration. This can be done via the following: . NET Core CLI.

What is database EnsureCreated?

EnsureCreated. EnsureCreated will create the database if it doesn't exist and initialize the database schema. If any tables exist (including tables for another DbContext class), the schema won't be initialized. C# Copy. // Create the database if it doesn't exist dbContext.Database.EnsureCreated();

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Is EF core faster than EF6?

Entity Framework (EF) Core, Microsoft's object-to-database mapper library for . NET Framework, brings performance improvements for data updates in version 7, Microsoft claims. The performance of SaveChanges method in EF7 is up to 74% faster than in EF6, in some scenarios.


1 Answers

I had the same problem you described. It looks like the SapientGuardian provider might simply have an incomplete implementation in this version.

The SapientGuardian project's readme file actually suggests using a more actively maintained provider such as Pomelo.EntityFrameworkCore.MySql. So I switched my project to Pomelo. Now the call to EnsureCreated creates the database as expected and does not have an authentication failed exception.

like image 59
mbnixon Avatar answered Sep 28 '22 04:09

mbnixon