EF Core 存储过程错误:'@p1' 附近的语法不正确 [关闭]
Posted
技术标签:
【中文标题】EF Core 存储过程错误:\'@p1\' 附近的语法不正确 [关闭]【英文标题】:EF Core Stored Procedure Error: Incorrect syntax near '@p1' [closed]EF Core 存储过程错误:'@p1' 附近的语法不正确 [关闭] 【发布时间】:2022-01-06 18:23:28 【问题描述】:在执行 API 调用时,我收到以下错误:
Microsoft.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@p1'
所以,我有以下存储过程:
/****** Object: StoredProcedure [dbo].[sprocGetDataForSummaryTable] Script Date: 30-Nov-21 00:23:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sprocGetDataForSummaryTable] @StartDate datetime2(7), @EndDate datetime2(7), @AccountName nvarchar(50) AS BEGIN
SET
NOCOUNT ON;
SELECT
a.Name,
DATEPART(YEAR, t.TransactionDate) AS Year,
DATEPART(MONTH, t.TransactionDate) AS Month,
SUM(t.Value) AS TotalValue,
c.Name AS CategoryName,
s.Name AS SubcategoryName
FROM
[PersonalMoneyTrackerDb].[dbo].[Transactions] AS t
INNER JOIN Categories AS c ON t.CategoryId = c.id
INNER JOIN Subcategories AS s ON t.SubcategoryId = s.Id
INNER JOIN Accounts as a ON t.AccountId = a.Id
WHERE
t.Type = 'Expense'
AND t.TransactionDate < @EndDate
AND t.TransactionDate > @StartDate
AND a.Name LIKE '%' + @AccountName + '%'
GROUP BY
a.Name,
DATEPART(YEAR, t.TransactionDate),
DATEPART(MONTH, t.TransactionDate),
c.Name,
s.Name
ORDER BY
DATEPART(YEAR, t.TransactionDate),
DATEPART(MONTH, t.TransactionDate),
c.Name;
END
GO
以及以下 .NET 6 EF 核心调用:
public async Task<List<SummaryTableEntity>> GetSummaries(string startDate, string endDate, string accountName)
return await _dbContext.SummaryTableEntities
.FromSqlInterpolated($"sprocGetDataForSummaryTable startDate endDate accountName")
.ToListAsync();
通过使用 SQL Server Profiler,我注意到它被翻译成:
exec sp_executesql N'sprocGetDataForSummaryTable @p0 @p1 @p2
',N'@p0 nvarchar(4000),@p1 nvarchar(4000),@p2 nvarchar(4000)',@p0=N'01/01/2020',@p1=N'01/01/2022',@p2=N''
我完全不知道这是什么问题,请帮忙。
【问题讨论】:
您缺少参数之间的逗号:startDate, endDate, accountName
【参考方案1】:
看起来你缺少逗号来分隔参数:
return await _dbContext.SummaryTableEntities
.FromSqlInterpolated($"sprocGetDataForSummaryTable startDate, endDate, accountName")
.ToListAsync();
【讨论】:
以上是关于EF Core 存储过程错误:'@p1' 附近的语法不正确 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
EF Core - 代码优先迁移:关键字“NOT”附近的语法不正确
无法使用 EF Core 或 LinqToDb EF Core Tools 或 ADO.NET 运行任何存储过程创建脚本