locked
required Help in integrating FromSql in generic way, using entityFramework core. RRS feed

  • Question

  • User410019758 posted

    Hello All,

    Write now I am creating one project using .net core and entityframeworkcore. For this  I have refered below article to implement generic repository in the code. 

    Generic Repository using EntityFrameworkCore . As per my project requirement i want to call stored procedures in some places. for this I tried to implement three addition methods in Igenericrepository class.

    IQueryable<T> ExecuteQuery(string spQuery, object[] parameters);
    T ExecuteQuerySingle(string spQuery, object[] parameters);
    int ExecuteCommand(string spQuery, object[] parameters);

    Now try to implement the methods in generic repository class. But not able to add it proper. Can anyone help me to implement these methods in the class. 
    Below is my code in generic class.

    public IQueryable<T> ExecuteQuery(string spQuery, object[] parameters)
    {
    IQueryable<T> queryable = _context.FromSql<T>(spQuery, parameters).ToList();///_context.FromSql<T>(spQuery, parameters).ToList();
    return queryable;
    
    }
    
    public T ExecuteQuerySingle(string spQuery, object[] parameters)
    {
    return _context.FromSql<T>(spQuery, parameters).FirstOrDefault();
    }
    
    public int ExecuteCommand(string spQuery, object[] parameters)
    {
    int result = 0;
    result = _context.FromSql<int>(spQuery, parameters).FirstOrDefault();
    catch { }
    return result;
    }
    
    

    Please help me to resolve this issue in the code.

    Thanks In Advance.
    Omkar.

    Friday, November 23, 2018 4:43 AM

All replies