0

I have the following code in my DbContext

protected override void OnModelCreating(ModelBuilder builder)
{
    // ...

    // Shorten key length for Identity
    builder.Entity<ApplicationUser>(entity => entity.Property(m => m.Id).HasMaxLength(127));
    builder.Entity<IdentityRole>(entity => entity.Property(m => m.Id).HasMaxLength(127));
    builder.Entity<IdentityUserLogin<string>>(entity =>
    {
        entity.Property(m => m.LoginProvider).HasMaxLength(127);
        entity.Property(m => m.ProviderKey).HasMaxLength(127);
    });
    builder.Entity<IdentityUserRole<string>>(entity =>
    {
        entity.Property(m => m.UserId).HasMaxLength(127);
        entity.Property(m => m.RoleId).HasMaxLength(127);
    });
    builder.Entity<IdentityUserToken<string>>(entity =>
    {
        entity.Property(m => m.UserId).HasMaxLength(127);
        entity.Property(m => m.LoginProvider).HasMaxLength(127);
        entity.Property(m => m.Name).HasMaxLength(127);
    });

I only want to apply these changes to the models if the server/provider being used is MySQL. What is the best way to do that?

This answer suggests the following which feels hacky

DbContext.Database.Connection.GetType().Name

This answer suggests using checks such as Database.IsSqlServer() which is fine but I cannot find a Database.IsMySqlServer() method anywhere

Please help. Thanks.

Harindaka
  • 4,658
  • 8
  • 43
  • 62
  • `Database.IsMySql()` – Ivan Stoev Mar 27 '19 at 08:11
  • @IvanStoev Can you let me know which nuget package or namespace this IsMySql method is defined as I do not see it in Microsoft.EntityFrameworkCore – Harindaka Mar 27 '19 at 08:21
  • When installed, every [database provider](https://learn.microsoft.com/en-us/ef/core/providers/) adds its own **extension** methods to some classes. So basically if you can see `UseMySql(…)` on `DbContextOptionsBuilder`, then you should see `IsMySql()` on `DatabaseFacade`. What about MySQL, currently there are 2 providers, I don't know which one you use - I see these methods added by [Pomelo.EntityFrameworkCore.MySql](https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql) – Ivan Stoev Mar 27 '19 at 08:31

0 Answers0