Search code examples
mysqlentity-framework-coreef-core-3.1pomelo-entityframeworkcore-mysqldb-first

EntityFramework Core 3.1 + with MySql or Pomelo rename table to use DBFirst


I have done two test projects, one with MySQL.EntityFramworkCore and another with Pomelo.EntityFrameworkCore and in both of them the same thing happened to me when obtaining the model from the database.

1.- Scaffold-DbContext 'server = x.x.x.x.x; user = xxxx; password = xxxx; database = name;' MySql.EntityFrameworkCore -OutputDir Models -f
2.- Scaffold-DbContext 'server = x.x.x.x.x; user = xxxx; password = xxxx; database = name;' Pomelo.EntityFrameworkCore.MySql -OutputDir Models -f

I have tables like the following in my database: brand_cars and when performing the dbFirst process. It changes my name to BrandCars.

In SQLServer exist the parameter -UseDatabaseNames, but in mysql I have not found it

In the connector options I have not found this option. https://mysqlconnector.net/connection-options/

Is there a possibility to force that you do not change the name of the database table when performing dbFirst?

The versions of my connectors are as follows:

-For the Pomelo project

Microsoft.EntityFrameworkCore.Tools 3.1.12

Pomelo.EntityFrameworkCore.MySql 3.2.4

Pomelo.EntityFrameworkCore.MySql.Design 1.1.2

-For the MySQL project

Microsoft.EntityFrameworkCore.Design 5.0.3

Microsoft.EntityFrameworkCore.Tools 5.0.3

MySql.EntityFrameworkCore 5.0.0


Solution

  • Works fine for me using Pomelo.EntityFrameworkCore.MySql in its 5.0.0-alpha.2 (or nightly build) release.

    I tested it using the following SQL:

    drop database if exists `So66530902`;
    create database `So66530902`;
    use `So66530902`;
    
    CREATE TABLE `ice_creams` (
        `IceCreamId` int NOT NULL AUTO_INCREMENT,
        `Name` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_IceCreams` PRIMARY KEY (`IceCreamId`)
    );
    

    I then executed the following command, using the --use-database-names parameter:

    dotnet ef dbcontext scaffold "server=127.0.0.1;uid=root;pwd=;port=3308;database=So66530902" Pomelo.EntityFrameworkCore.MySql -c Context -o Models --use-database-names
    

    The result is the following class:

    using System;
    using System.Collections.Generic;
    
    #nullable disable
    
    namespace IssueConsoleTemplate.Models
    {
        public partial class ice_cream
        {
            public int IceCreamId { get; set; }
            public string Name { get; set; }
        }
    }
    

    It should be mentioned that if you don't want EF Core to change the database table names in any way, you should also add the --no-pluralize flag in addition to --use-database-names.