![在此处输入图像说明] [1]我已经获得了最新的Mysql连接器,允许您使用asp.net webapi来使用Visual Studio Entity Framework设计器。并使用以下代码调用商店程序
public static class Class1
{
public static List<T> ExecuteStoredProcedure<T>(this DbContext dbContext, string storedProcedureName,List<object> parameters)
{
string storedProcedureCommand = "CALL " + storedProcedureName + "(";
List<object> augmentedParameters = parameters.ToList();
storedProcedureCommand = AddParametersToCommand(storedProcedureCommand, augmentedParameters);
storedProcedureCommand += ");";
return dbContext.Database.SqlQuery<T>(storedProcedureCommand).ToList<T>();
}
public static List<T> ExecuteStoredRecursiveProcedure<T>(this DbContext dbContext, string storedProcedureName, params object[] parameters)
{
string storedProcedureCommand = "SET max_sp_recursion_depth = " + maxRecursionCount + "; CALL " + storedProcedureName + "(";
List<object> augmentedParameters = parameters.ToList();
storedProcedureCommand = AddParametersToCommand(storedProcedureCommand, augmentedParameters);
storedProcedureCommand += ");";
return dbContext.Database.SqlQuery<T>(storedProcedureCommand).ToList<T>();
}
private static string AddParametersToCommand(string storedProcedureCommand, List<object> augmentedParameters)
{
for (int i = 0; i < augmentedParameters.Count(); i++)
{
storedProcedureCommand = AddParameterToCommand(storedProcedureCommand, augmentedParameters, i);
}
return storedProcedureCommand;
}
private static string AddParameterToCommand(string storedProcedureCommand, List<object> augmentedParameters, int i)
{
if (augmentedParameters[i].GetType() == typeof(string))
{
storedProcedureCommand += "'";
}
storedProcedureCommand += (augmentedParameters[i].ToString());
if (augmentedParameters[i].GetType() == typeof(string))
{
storedProcedureCommand += "'";
}
if (i < augmentedParameters.Count - 1)
{
storedProcedureCommand += ",";
}
return storedProcedureCommand;
}
public static string maxRecursionCount { get; set; }
}
这里我用零参数调用ExecuteStoredProcedure方法但是它会导致上述错误&#34;数据读取器有多个字段。多个字段对EDM原语或枚举类型无效。&#34;
public List<decimal> Getcollection()
{
List<object> listobj=new List<object>();
var x= db.ExecuteStoredProcedure<decimal>("MangerModule",listobj);
var list = x.ToList();
return list;
//collection collection = db.collections.Find(id);
//if (collection == null)
//{
// throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
//}
//return collection;
}
答案 0 :(得分:1)
我使用下面的代码来解决我的问题:
public virtual List<columns> Getcollection()
{
string SQLQuery = "call MangerModule();";
var objectContext = ((IObjectContextAdapter)db).ObjectContext;
List<object> listobj = new List<object>();
List<columns> data = objectContext.ExecuteStoreQuery<columns>(SQLQuery).AsQueryable().ToList();
return data;
}