No mapping to a relational type can be found for the CLR type 'Int32[]'

戏子无情 提交于 2019-11-30 19:02:59

You are falling into a typical params object[] trap.

First, let see what's the type of the expression

var parameters = new[] { customerProductDelivery.CustomerOrderID,customerProductDelivery.DeliveryQty }

Since both elements are of type int, the inferred type is int[].

Now, there are 2 ExecuteSqlCommand method overloads which allow you to pass parameters separately - one receiving params object[] and second receiving IEnumerable<object>.

Although IEnumerable<T> is covariant, covariance does not work for value types, so IEnumerable<int> cannot be treated as IEnumerable<object>, hence the second overload does not match.

So the only valid overload is the one receiving params object[]. But since int[] cannot be cast to object[], the compiler treats it as single object, thus emitting something like new object[] { parameters }.

The net effect is that the EF method receives single parameter with int[] type value and generates the exception in question.

With that being said, it could be fixed in many ways.

One is to remove new [] { } from the call, which is the whole idea of params object[] construct:

_context.Database.ExecuteSqlCommand(
    "Update_ProductOrderAndStock @p0, @p1", customerProductDelivery.CustomerOrderID, customerProductDelivery.DeliveryQty);

Another is to replace the new [] with the explicit new object[].

Yet another one is to utilize the EF Core introduced String interpolation in FromSql and ExecuteSqlCommand:

_context.Database.ExecuteSqlCommand(
    $"Update_ProductOrderAndStock {customerProductDelivery.CustomerOrderID}, {customerProductDelivery.DeliveryQty}");

Please update the code

from

    _context.Database.ExecuteSqlCommand(
                   "sp_UpdateProductOrderAndStock @p0, @p1", parameters: 
                     new[] 
                     { 
                        customerProductDelivery.CustomerOrderID,customerProductDelivery.DeliveryQty
                      });

to

List<SqlParameter> pc = new List<SqlParameter>
{
   new SqlParameter("@customerOrderID", customerProductDelivery.CustomerOrderI),
   new SqlParameter("@qty", customerProductDelivery.DeliveryQty)
}

_context.Database.ExecuteSqlCommand("sp_UpdateProductOrderAndStock @customerOrderID, @qty", pc.ToArray());

Another Answer

var p0 = new SqlParameter("@customerOrderID", customerProductDelivery.CustomerOrderID);
        var p1 = new SqlParameter("@qty", customerProductDelivery.DeliveryQty);
        _context.Database.ExecuteSqlCommand("sp_UpdateProductOrderAndStock @customerOrderID, @qty", p0, p1);

        //or  var p0 = new SqlParameter("@p0", customerProductDelivery.CustomerOrderID);
        //or  var p1 = new SqlParameter("@p1", customerProductDelivery.DeliveryQty);
        // or _context.Database.ExecuteSqlCommand("sp_UpdateProductOrderAndStock @p0, @p1", p0, p1); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!