1

I am developing a .Net Core Web Application using Entity Framework and MySQL and when I try to register as a user, I get the following message.

An unhandled exception occurred while processing the request.
InvalidOperationException: Connection must be valid and open to commit transaction
MySql.Data.MySqlClient.MySqlTransaction.Commit()

This is my DB Context

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

    }
}

}

And this is my Startup code

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();

    }

--UPDATE----

I Found out that the Authentication mechanism is done by Ajax not EF, the problem seems to stem from this line of code.

    public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
   PROBLEM LINE====>         var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation(3, "User created a new account with password.");
                return RedirectToLocal(returnUrl);
            }
            AddErrors(result);
        }
2
  • are you sure you have a valid connection string in appsettings.json for "DefaultConnection"? Mar 30, 2017 at 18:37
  • Yes, the connection string works fine. For other data, it actually works fine, its only with the Authentication that it gives such problems.
    – k3ra
    Mar 30, 2017 at 19:48

1 Answer 1

0

I was having this exact same problem amonst a few others the other night. It's some kind of issue with async and the Oracle MySQL provider. If you override your DbContext's dispose method and put a breakpoint in there, you see the context gets disposed before some of the sign in steps are done. I found another SO question (can't find it right now on mobile), that had similar issues and recommended replacing Oracle MySQL package with this one:

https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

This fixed the problem and another problem I was having related to handling of varbinary data types longer than 255 when adding migrations.

Also remember the DbContext is loaded into the DI container in the "scoped" lifetime (by default), which means it is a single instance per http request, so make sure your code or any middleware you have in place isn't disposing the DbContext after getting it from the service container.

1
  • YES! Thank you so much. I agree, I think that because its in pre-release, its still buggy. but Pomelo.EntityFrameworkCore.MySql Library works fine! Thanks!
    – k3ra
    Mar 31, 2017 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.