I have the following code in .NET 6 that calls a stored procedure through Entity Framework Core using the dbContext
:
public async Task<IEnumerable<Portfolio>> GetPortfolioAsync(
DateTime portfolioDate,
string clientName,
DateTime reportDate,
IEnumerable<string> portfolioNames)
{
var tempTable = new DataTable();
tempTable.Columns.Add("Item", typeof(string));
foreach (var portfolioName in portfolioNames)
{
tempTable.Rows.Add(portfolioName);
}
SqlParameter[] parameters =
{
new SqlParameter("@Client",
SqlDbType.VarChar) { Direction = ParameterDirection.Input, Value = client },
new SqlParameter("@PortfolioDate",
SqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = portfolioDate },
new SqlParameter("@ReportDate",
SqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = reportDate },
new SqlParameter("@PortfolioNames",
SqlDbType.Structured) { Direction = ParameterDirection.Input, Value = table }
};
var getPorfolio = await this.dbContext.Database.ExecuteSqlRawAsync("exec GetPortfolio", parameters);
return getPorfolio.List();
}
The SQL Server stored procedure is as follows:
ALTER PROCEDURE [dbo].[GetPortfolio]
@Client varchar(40),
@PortfolioDate datetime,
@ReportDate datetime,
@PortfolioNames dbo.StringList READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Portfolio
WHERE Client = @Client
AND PortfolioDate = @PortfolioDate
AND ReportDate <= @ReportDate
AND PortfolioName IN @PortfolioNames
SET NOCOUNT OFF;
END
When I debug, I get the following error:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.
Newtonsoft.Json.JsonReaderException: Invalid character after parsing property name. Expected ':' but got: 1. Path '', line 1, position 8.
at Newtonsoft.Json.JsonTextReader.ParseProperty()
at Newtonsoft.Json.JsonTextReader.ParseObject()
at Newtonsoft.Json.JsonReader.ReadAndAssert()
etc.
I have tried different options, but I always get the same error. Any ideas?