The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types

c# entity-framework

Question

![enter image description here][1]I've got the latest Mysql connector that allows you to use the Visual Studio Entity Framework designer using asp.net webapi. and call the store procedure using below code

 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; }
}

Here i am calling the ExecuteStoredProcedure method with zero paramter but it's siplaying the above error "The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types."

  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;
    }
1
0
5/29/2014 7:14:17 AM

Accepted Answer

I've used below code to solve my issue:

     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;
    }
2
1/24/2018 3:21:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow