简体   繁体   中英

Entity Framework 5 stored procedure with multiple result sets

I'm currently coding with asp.net mvc 4.5 and EF5 Beta 2 and I have a stored procedure which returns multiple result sets. I've found this site and it says that the newer version (which I'm using) already has support for multiple result sets.

Now I can't seem to find that support. As I'm fairly new to the EF altogether, I hope I'm not doing something wrong.

I have no corresponding entities in my database for the result sets created.

The core EF libraries support multi result set procedures. Unfortunately the designer does not -- and it's not clear if it will upon release.

I too found the documentation a bit sparse, particularly for returning multiple entity types (as opposed to complex types). Fortunately, manually editing the EDMX isn't too complicated. I wrote up a blog post on topic ....

Entity Framework 5 – Multiple Entity-Typed Result Sets from a Stored Procedure ( note, my server may take a few minutes for the disks to spin up as not too many people traffic my humble little blog ).

The short of it is in the CSDL section ..

<edmx:ConceptualModels>
  <Schema Namespace="myModel" ...>
    <EntityContainer Name="myModelEntities" ....>
    ......
    <!-- 
     this is what “function import” wrote, that I’m overwriting…         
     FunctionImport Name="MyMARS_Proc" ReturnType="Collection(myModel.Table_A)"/>
     -->
     <FunctionImport Name="MyMARS_Proc" >
        <ReturnType Type="Collection(myModel.Table_A)" EntitySet="Table_As"/>
        <ReturnType Type="Collection(myModel.Table_B)" EntitySet="Table_Bs"/>
     </FunctionImport> 

Then in the MSL (CS Mapping section) you'll want...

<edmx:Mappings>
  <Mapping Space="C-S" ....>
    <EntityContainerMapping  ....>

       <FunctionImportMapping FunctionImportName="MyMARS_Proc"
                               FunctionName="myModel.Store.MyMARS_Proc">
            <ResultMapping>
                <EntityTypeMapping  TypeName="myModel.Table_A"/>
            </ResultMapping>
            <ResultMapping>
                <EntityTypeMapping TypeName="myModel.Table_B"/>
            </ResultMapping>
       </FunctionImportMapping>

That is quite interesting question. I played with .NET 4.5 Beta / VS11 Beta for a while and I have two observations:

  • It looks like there is no support in designer for multiple result sets - everything must be mapped manually in EDMX opened as XML
  • It looks like the former article about June CTP 2011 is no longer valid because MSL doesn't allow declaring multiple ResultMapping to map different result sets and each ResultMapping can map only single result set

EDIT:

I was wrong. It works at runtime. Only designer complains about EDMX validation but MSL itself accepts multiple ResultMapping elements. The original walkthrough linked in the question is still valid.

I did this code

The procedure as following

           alter procedure dbo.proc_getmorethanonetable

          as
       select * from dbo.Orders
      select * from dbo.Items
     select * 
      from dbo.Users u
      inner join dbo.Orders o 
        on o.User_ID = u.User_ID
  go

The Code is so Simple Here we go

        using (var db = new EF_DEMOEntities())
              {
                   var cmd = db.Database.Connection.CreateCommand();
                    cmd.CommandText = "[dbo].[proc_getmorethanonetable]";
                     try
                       {
                            db.Database.Connection.Open();
                            using (var reader = cmd.ExecuteReader())
                           {
                                 var orders =            
                                 ((IObjectContextAdapter)db).ObjectContext.Translate<Order>(reader);
                                 GridView1.DataSource = orders.ToList();
                                 GridView1.DataBind();
                                 reader.NextResult();
                                var items =  
                           ((IObjectContextAdapter)db).ObjectContext.Translate<Item>(reader);
                              GridView2.DataSource = items.ToList();
                              GridView2.DataBind();


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

and here you go :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM