EntityFrameworkExtras icon indicating copy to clipboard operation
EntityFrameworkExtras copied to clipboard

How do we used it with stored proc that has multiple result sets?

Open xavierjohn opened this issue 10 years ago • 4 comments

How do we used it with stored proc that has multiple result sets?

As mentioned in https://msdn.microsoft.com/en-us/data/jj691402.aspx

Thanks.

xavierjohn avatar Feb 26 '15 19:02 xavierjohn

I need the answer for this as well. I see the ExecuteStoredProcedure(proc) but how do I grab the result? my result does have multiple result sets as well.

haldiggs avatar May 28 '15 21:05 haldiggs

Unfortunately Entity Framework Extras doesn't support multiple record sets. It only returns the first collection.

Fodsuk avatar May 30 '15 21:05 Fodsuk

One option could be to wrap the DbDataReader, something like

    public static DbDataReader ExecuteReader(this Database database, object storedProcedure)
    {
        if (storedProcedure == null)
            throw new ArgumentNullException("storedProcedure");

        var info = StoredProcedureParser.BuildStoredProcedureInfo(storedProcedure);

        var cmd = database.Connection.CreateCommand();
        cmd.CommandText = info.Sql;
        cmd.Parameters.AddRange(info.SqlParameters);

        return cmd.ExecuteReader();
    }

xavierjohn avatar Jul 09 '15 18:07 xavierjohn

I was able to use xavierjohn's suggestion successfully in a fork. In case the next person to find this on Google is as clueless as I was ObjectContext.Translate is used to translate the DbDataReader results into Entity objects. This is the snippet from my code:


using (System.Data.Common.DbDataReader reader = EntityFrameworkExtras.EF6.DatabaseExtensions.ExecuteReader(this.Database, proc))
    {
        Structure[] Nodes = ((IObjectContextAdapter)this).ObjectContext.Translate<Structure>(reader, "Structures", MergeOption.NoTracking).ToArray();
        reader.NextResult();
        StructureLink[] Edges = ((IObjectContextAdapter)this).ObjectContext.Translate<StructureLink>(reader, "StructureLinks", MergeOption.NoTracking).ToArray();

        retval = new NetworkDetails(NodeObjects, ChildObjects, Edges);
        }

jamesra avatar Apr 02 '16 00:04 jamesra