具有命名参数的EF6 SqlQuery

时间:2018-12-07 10:03:33

标签: c# asp.net sql-server entity-framework entity-framework-6

我有一个接受一些参数的存储过程,但是我找不到正确的语法。根据各种消息来源,以下方法应该起作用:

var results = base.Context.Database.SqlQuery<UserSummary>("exec FindPatients forenames = @forenames, surname = @surname",
new SqlParameter("@forenames", string.IsNullOrWhiteSpace(forenames) ? (object)DBNull.Value : forenames),
new SqlParameter("@surname", string.IsNullOrWhiteSpace(surname) ? (object)DBNull.Value : surname));

调用.ToList()时,出现SqlException:Incorrect syntax near '='.

我尝试删除该exec:

var results = base.Context.Database.SqlQuery<UserSummary>("FindPatients @forenames, @surname",
new SqlParameter("@forenames", string.IsNullOrWhiteSpace(forenames) ? (object)DBNull.Value : forenames),
new SqlParameter("@surname", string.IsNullOrWhiteSpace(surname) ? (object)DBNull.Value : surname));

哪个产生The SqlParameter is already contained by another SqlParameterCollection

我不想使用编号的参数并将{1},{2}等传递给sproc,因为我需要10个左右的参数,这是维护的噩梦。我想念什么吗?

0 个答案:

没有答案