Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EF Core 8 scaffolds BIT(1) columns with default value as nullable bool #1931

Open
gao-artur opened this issue Jul 12, 2024 · 0 comments
Open

Comments

@gao-artur
Copy link

gao-artur commented Jul 12, 2024

The issue

EF Core 8 made the long awaited change and now scaffolds boolean columns with default value as bool instead of bool?. However, I found it doesn't work with Pomelo.EntityFrameworkCore.MySql provider. I believe that's because EF Core can't parse the default b'0' as false and b'1' as true.

Currently, as a workaround, I register a custom IDatabaseModelFactory in IDesignTimeServices with this code to make it work:

public override DatabaseModel Create(string connectionString, DatabaseModelFactoryOptions options)
{
    var databaseModel = base.Create(connectionString, options);
    SetBoolDefault(databaseModel);
    return databaseModel;
}

public override DatabaseModel Create(DbConnection connection, DatabaseModelFactoryOptions options)
{
    var databaseModel = base.Create(connection, options);
    SetBoolDefault(databaseModel);
    return databaseModel;
}

private void SetBoolDefault(DatabaseModel databaseModel)
{
    foreach (var dbTable in databaseModel.Tables)
    {
        foreach (var dbColumn in dbTable.Columns)
        {
            if (dbColumn.DefaultValueSql == "b'0'")
            {
                dbColumn.DefaultValue = false;
            }
            else if (dbColumn.DefaultValueSql == "b'1'")
            {
                dbColumn.DefaultValue = true;
            }
        }
    }
}

Further technical details

MySQL version: 8.0.28
Operating system: Win10
Pomelo.EntityFrameworkCore.MySql version: 8.0.2
Microsoft.AspNetCore.App version: 8.0.7

Other details about my project setup:

@gao-artur gao-artur changed the title EF Core 8 scaffolds bool columns with default value as nullable EF Core 8 scaffolds BIT(1) columns with default value as nullable bool Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant