zzzprojects / EntityFrameworkExtras

EntityFrameworkExtras provides some useful additions to EntityFramework such as executing Stored Procedures with User-Defined Table Types and Output Parameters.
https://entityframework-extras.net/
MIT License
80 stars 40 forks source link

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

Open xavierjohn opened 9 years ago

xavierjohn commented 9 years ago

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.

haldiggs commented 9 years ago

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.

Fodsuk commented 9 years ago

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

xavierjohn commented 9 years ago

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();
    }
jamesra commented 8 years ago

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