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

c# entity-framework-core

Question

When I execute a SQL Server stored procedure from Entity Framework Core (v2.0) in my ASP.NET Core project, I get this exception:

InvalidOperationException: no mapping to a relational type can be found for the CLR type 'Int32[]'

The SQL Server stored procedure code looks like this:

CREATE PROCEDURE [dbo].[sp-UpdateProductOrderAndStock]
    @customerOrderID INT,
    @qty INT
AS
    DECLARE @customerProductID INT
    SET @customerProductID = (SELECT CustomerProductID 
                              FROM dbo.CustomerOrder 
                              WHERE ID = @customerOrderID)

    SET NOCOUNT ON;  

    UPDATE dbo.CustomerOrder
    SET CompletedQty = CompletedQty + @qty
    WHERE ID = @customerOrderID;

    UPDATE dbo.CProductStation
    SET Qty = Qty - @qty
    WHERE CustomerProductID = @customerProductID AND StationNo = 0;

ASP.NET Core data model as below:

public class CustomerProductDelivery
{
    public int ID { get; set; }
    public int CustomerOrderID { get; set; }
    public DateTime DeliveryDate { get; set; }
    public string DeliveryNo { get; set; }
    public int DeliveryQty { get; set; }
    public CustomerOrder CustomerOrder { get; set; }
}

ASP.net core Controller code to execute the stored procedure:

_context.Database.ExecuteSqlCommand("sp-UpdateProductOrderAndStock @p0, @p1", parameters: new[] { customerProductDelivery.CustomerOrderID,customerProductDelivery.DeliveryQty });
1
11
8/6/2018 9:15:39 AM

Accepted Answer

TLDR

Use string interpolation

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

Detailed answer:

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}");
17
3/26/2020 2:34:43 PM

Popular Answer

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());


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow