How to pass non-optional NULL parameters to a Stored Proc using OrmLite

牧云@^-^@ 提交于 2019-12-06 00:41:41

It's not supported with SqlScalar. When you look at the code then you can see that SqlScalar methods from class ServiceStack.OrmLite.OrmLiteReadExtensions execute SetParameters method responsible for adding parameters to query with second parameter(excludeNulls) equal true I don't know why- mythz should answer for this ;).
If you want to fix it then you have change all SqlScalar methods to invoke SetParameters with true and SetParameters method should look like following(must support DBNull.Value not null)

private static void SetParameters(this IDbCommand dbCmd, object anonType, bool excludeNulls)
    {
        dbCmd.Parameters.Clear();
        lastQueryType = null;
        if (anonType == null) return;

        var pis = anonType.GetType().GetSerializableProperties();
        foreach (var pi in pis)
        {
            var mi = pi.GetGetMethod();
            if (mi == null) continue;

            var value = mi.Invoke(anonType, new object[0]);
            if (excludeNulls && value == null) continue;

            var p = dbCmd.CreateParameter();
            p.ParameterName = pi.Name;
            p.DbType = OrmLiteConfig.DialectProvider.GetColumnDbType(pi.PropertyType);
            p.Direction = ParameterDirection.Input;
            p.Value = value ?? DBNull.Value; // I HAVE CHANGED THAT LINE ONLY
            dbCmd.Parameters.Add(p);
        }
    }

When you change code then you can set null for parameters in the following way:

var result = db.SqlScalar<int>("EXEC DummyScalar @Times", new { Times = (int?)null });

In my opinion you can describe it as a defect on github and I can make pull request.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!