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

c# entity-framework

Question

! [Insert a description of the image] [1] I have the most recent Mysql connection, which enables you to use the Entity Framework designer in Visual Studio by using the asp.net webapi. and use the code below to invoke the store method.

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

I'm using the ExecuteStoredProcedure method here with no parameters, however the issue mentioned above is still appearing "There are several fields on the data reader. For EDM primitive or enumeration types, multiple fields are invalid."

  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 used the code below to fix the problem:

     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