How to get Generic List from stored procedure by using entity framework ?

Please explain with a suitable example...

Last updated:7/2/2019 11:34:57 PM

1 Answers

Anonymous User
Anonymous User

Try to use it with entity framework.

 public void ExecuteList<T>(out List<T> obj, string sql, params object[] parameters) where T : class

        {
            var db = _context;
            var cmd = db.Database.Connection.CreateCommand();
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddRange(parameters);
            try
            {
                db.Database.Connection.Open();
                using (var reder = cmd.ExecuteReader())
                {
                    obj = ((IObjectContextAdapter)db).ObjectContext.Translate<T>(reder).ToList();

                }
            }
            finally
            {
                db.Database.Connection.Close();
                cmd.Dispose();
            }

        }


Answer