I am trying to get this working, but it fails with the following error when I try to fill my dataset (it’s an Azure SQL database, and the client is on .NET 7.0):
No mapping exists from object type System.DateOnly to a known managed provider native type.
My stored procedure is:
CREATE PROCEDURE [dbo].[sproc_WMSSummaryStats]
@userId INT,
@datefrom date,
@dateto date
AS
.....
My Web API endpoint looks like this:
[HttpGet("homePageSummaryData")]
public string homePageSummaryData(int user, int warehouseId, int zoneId, DateOnly startDate, DateOnly endDate, string licenseKey)
{
// ....
}
I call the stored procedure from the Web API code like this:
cmd.Connection = conn;
conn.Open();
cmd.Parameters.AddWithValue("@userID", user);
cmd.Parameters.AddWithValue("@datefrom" , startDate);
cmd.Parameters.AddWithValue("@dateto", endDate);
I have tried to test the end point in Swagger, but it’s generating the same error.
3
Since version 5.1.0 the Microsoft.Data.SqlClient
NuGet package does support DateOnly
and TimeOnly
as value-types for use with SqlParameter.Value
and elsewhere.
…so you’ll only get the “No mapping exists from object type System.DateOnly to a known managed provider native type.” error if you’re using an outdated version of Microsoft.Data.SqlClient
.
As of June 2024, the current version is 5.2.1, and v5.1.0 has been out for almost 18 months now.