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

MySQL error when using BatchUpdateAsync() method #1431

Open
MayGodBlessYouAMen opened this issue Mar 8, 2024 · 2 comments
Open

MySQL error when using BatchUpdateAsync() method #1431

MayGodBlessYouAMen opened this issue Mar 8, 2024 · 2 comments
Labels

Comments

@MayGodBlessYouAMen
Copy link

My project environment:
mysql8, .net8, efcore8, EFCore.BulkExtensions 8.0.2
The code I use like this:
dbContext.Users.Where(x => x.State == 0).BatchUpdateAsync(x => new User { State = 1 });
error message:
"Unable to cast object of type 'Microsoft.Data.SqlClient.SqlParameter' to type 'MySqlConnector.MySqlParameter'."

When I looked at the source code of the EFCore.BulkExtensions project, I found that the method does not support MySQL:
public static (string, List<object>) GetSqlUpdate<T>(IQueryable<T> query, DbContext context, Type type, Expression<Func<T, T>> expression)

var databaseType = SqlAdaptersMapping.GetDatabaseType();
        if (databaseType == SqlType.PostgreSql)
        {
           ...
            sqlParameters = npgsqlParameters;
        }
  
        return (resultQuery, sqlParameters);

Another method supports MySQL, but I don’t want to use it because I use the Expression parameter:
public static (string, List<object>) GetSqlUpdate(IQueryable query, DbContext context, Type type, object? updateValues, List<string>? updateColumns)

        var databaseType = SqlAdaptersMapping.GetDatabaseType();
        if (databaseType == SqlType.PostgreSql)
        {
             ...
            sqlParameters = npgsqlParameters;
        }
        else if (databaseType == SqlType.MySql)
        {
            ...
            sqlParameters = mysqlParameters;
        }

        return (resultQuery, sqlParameters);

Can you add MySQL support for the method
BatchUtil.GetSqlUpdate<T>(IQueryable<T> query, DbContext context, Type type, Expression<Func<T, T>> expression)

@borisdj
Copy link
Owner

borisdj commented Mar 12, 2024

Code refactored with the fix, will be published with next nuget release.

@borisdj borisdj added the bug label Mar 12, 2024
@borisdj
Copy link
Owner

borisdj commented Mar 12, 2024

Also have you tried new EF native method ExecuteUpdate.
Batch ops are deprecated, Lib focus in mainly on Bulk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants