UpdateOnlyFields generating wrong SQL

Hi,
we are using the UpdateOnlyFields Method in a .Net 4.8 application with ServiceStack.OrmLite in version 6.11.0.
With MSSQL our code works well. Switching to MySql/MariaDb, the generated SQL looks like this:

UPDATE `RessourceElement` SET `ContentString`= WHERE (`Key` = @0)

Our code looks like this:

try
{
    int updated = conn.UpdateOnlyFields(model, onlyFields, @where, commandFilter);
    if (updated != 1 && throwWhenNotFound)
    {
        throw new Exception("Datenbank-Operation UpdateOnly fehlgeschlagen - es wurde kein passendes Element in der Datenbank aktualisiert. Last SQL: " + conn.GetLastSql());
    }
}
catch (Exception e)
{
    Log.Error("Datenbank-Operation UpdateOnly fehlgeschlagen. Last SQL: " + conn.GetLastSql(), e);
    throw;
}

The GetLastSql() is empty in the catch block.
The variables have the following values:

model = new RessourceElement{ ContentString = NewContentString}
onlyFields = r => new {r.ContentString}
where = x => x.Key == "*****"

We already tried with different databases / servers. No difference.
Any hints or ideas what could be the issue?

Wont know without a repro, maybe the Exception is thrown before the CommandText is populated on the command?

Can you try debugging into the Framework sources to identify the issue?

Today I can not debug into the sources.
If I add a commandFilter and output the CommandText there, I get the SQL shown above.
I will update on monday, when I have debugged further.

1 Like

I debugged into the sources now.
In ServiceStack.OrmLite.OrmLiteDialectProviderBase in Line 1336, an exception is thrown:

In the catch block, this exception is handeled and the execution continues and produces an invalid SQL.

Stack:
MySql.Data.MySqlClient.MySqlException: “Only MySqlParameter objects may be stored”

Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
MySql.Data.MySqlClient.MySqlParameterCollection.Add(object)
ServiceStack.OrmLite.DbDataParameterExtensions.AddParam(ServiceStack.OrmLite.IOrmLiteDialectProvider, System.Data.IDbCommand, object, ServiceStack.OrmLite.FieldDefinition, System.Action<System.Data.IDbDataParameter>) in SqlExpression.cs
ServiceStack.OrmLite.DbDataParameterExtensions.AddUpdateParam(ServiceStack.OrmLite.IOrmLiteDialectProvider, System.Data.IDbCommand, object, ServiceStack.OrmLite.FieldDefinition) in SqlExpression.cs
ServiceStack.OrmLite.DbDataParameterExtensions.GetUpdateParam(ServiceStack.OrmLite.IOrmLiteDialectProvider, System.Data.IDbCommand, object, ServiceStack.OrmLite.FieldDefinition) in SqlExpression.cs
ServiceStack.OrmLite.OrmLiteDialectProviderBase.PrepareUpdateRowStatement(System.Data.IDbCommand, object, System.Collections.Generic.ICollection) in OrmLiteDialectProviderBase.cs

Perhaps also important: We are using mysqlconnector as dialect.

Not clear what’s causing the Exception but you can configure OrmLite to throw on Exceptions with:

OrmLiteConfig.ThrowOnError = true;

This default will be changed to throwing in the next release.

We were not able to solve the problem.
But we were able to switch completely to MS SQL, and there we do not have the issue any more.
Thanks

1 Like