0

如何将参数传递给使用 EF6 返回多条记录的存储过程 - DbContextSQL Server 2016 存储过程具有加密列?

使用 EF 6.1.3、SQL Server 2016 - AlwaysEncrypted(确定性)

几篇文章讨论了使用 ADO .NET 4.6 通过使用命令、参数对象来完成此任务,但我无法从我的 EF 中找到方法DbContext

4

1 回答 1

0
//make sure to provide the actual datatype and size on the parameter   
//you can use reflection on the entity to dynamically get/set the property info by a common function.

var paratmeterNames = new StringBuilder();
string paratmeterNamesUpdated = null;

if (lastName != null)
{
    var lastNameParam = new SqlParameter("@lastName", SqlDbType.VarChar, 60) {Value = lastName};
    parameterList.Add(lastNameParam);
    paratmeterNames.Append("@lastName,");
}

if (firstName != null)
{
    var firstNameParameter = new SqlParameter("@firstName", SqlDbType.VarChar, 30) {Value = firstName};
    parameterList.Add(firstNameParameter);
    paratmeterNames.Append("@firstName,");
}

if (paratmeterNames != null && paratmeterNames.Length > 0)
{
    paratmeterNamesUpdated = paratmeterNames.ToString().TrimEnd(',');
}

var result = _context.Database.SqlQuery<T>("exec sp_name " + paratmeterNamesUpdated, parameterList.ToArray()).ToList();

//this will make below **calls to SQL DB:**
//exec sp_describe_parameter_encryption N'exec sp_name @lastName,@firstName',N'@lastName varchar(60),@firstName varchar(30)'
//exec sp_executesql N'exec sp_name @lastName,@firstName',N'@lastName varchar(60),@firstName varchar(30)',@lastName=0x01A2256C34F97,@firstName=0x018EE4BD11BA7
于 2017-08-16T11:12:20.540 回答