Practical Introduction To Entity Framework: Day 4

The following are my previous articles on the fundamentals of Entity Framework: 

To start with this article please read all 3 previous articles to understand better or see this video https://www.youtube.com/v/b6vTIiBNcJ0

I am assuming you are good in Entity Framework now, so let's start.

How to Return Multiple Result Set or Data set in Entity Framework

Many developers do not know when they use Entity Framework whether it is very easy to implement and after updating the edmx whether the changes are still present.

Step 1

Create a normal Stored Procedure that will return multiple select statements.

 

  1. CREATE PROCEDURE GetCustomerProductDetails  
  2. AS  
  3. BEGIN  
  4. SELECT Id, Name from Customer  
  5. select ProductID, ProductName from product 
    END 
    GO  

Step 2

Create a Web project and add an ADO Entity Model as in the following:


 

 

 

 
Step 3

Now select the procedure you want to use for returning multiple result sets.


Step 4

Open the edmx file in the XML editor as in the following screens.




Step 5

Add multiple return types as per your requirements in the Function import Node and add Multiple Complex Types also.

 

  1. <edmx:ConceptualModels>  
  2. <SchemaNamespaceSchemaNamespace="MyModel"Alias="Self"p1:UseStrongSpatialTypes="false"xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation"  
  3. xmlnsxmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation"xmlns="http://schemas.microsoft.com/ado/2009/11/edm">  
  4. <EntityContainerNameEntityContainerName="CustomerManagementEntities"p1:LazyLoadingEnabled="true">  
  5. <FunctionImportNameFunctionImportName="GetCustomerProductDetails">  
  6. <ReturnTypeTypeReturnTypeType="Collection(MyModel.GetCustomerProductDetails_Result1)" />  
  7. <ReturnTypeTypeReturnTypeType="Collection(MyModel.GetCustomerProductDetails_Result2)" />  
  8. </FunctionImport>  
  9. </EntityContainer>  
  10. <ComplexTypeNameComplexTypeName="GetCustomerProductDetails_Result1">  
  11. <PropertyTypePropertyType="Int32"Name="Id"Nullable="false" />  
  12. <PropertyTypePropertyType="String"Name="Name"Nullable="true"MaxLength="50" />  
  13. </ComplexType>  
  14. <ComplexTypeNameComplexTypeName="GetCustomerProductDetails_Result2">  
  15. <PropertyTypePropertyType="Int32"Name="ProductID"Nullable="false" />  
  16. <PropertyTypePropertyType="String"Name="ProductName"Nullable="true"MaxLength="50" />  
  17. </ComplexType>   
    </
    Schema> 
    </edmx:ConceptualModels>  

Check the difference between normal and multiple result sets in the following screen for Return Type and Complex Type.


Step 6

Add Multiple Result Mapping as per your requirements in the Function import Mapping.

 

  1. <!-- C-S mapping content -->  
  2. <edmx:Mappings>  
  3. <MappingSpaceMappingSpace="C-S"xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">  
  4. <EntityContainerMappingStorageEntityContainerEntityContainerMappingStorageEntityContainer="MyModelStoreContainer"  
  5. CdmEntityContainer="CustomerManagementEntities">  
  6. <FunctionImportMappingFunctionImportNameFunctionImportMappingFunctionImportName="GetCustomerProductDetails"  
  7. FunctionName="MyModel.Store.GetCustomerProductDetails">  
  8. <ResultMapping>  
  9. <ComplexTypeMappingTypeNameComplexTypeMappingTypeName="MyModel.GetCustomerProductDetails_Result1">  
  10. <ScalarPropertyNameScalarPropertyName="Id"ColumnName="Id" />  
  11. <ScalarPropertyNameScalarPropertyName="Name"ColumnName="Name" />  
  12. </ComplexTypeMapping>  
  13. </ResultMapping>  
  14. <ResultMapping>  
  15. <ComplexTypeMappingTypeNameComplexTypeMappingTypeName="MyModel.GetCustomerProductDetails_Result2">  
  16. <ScalarPropertyNameScalarPropertyName="ProductID"ColumnName="ProductID" />  
  17. <ScalarPropertyNameScalarPropertyName="ProductName"ColumnName="ProductName" />  
  18. </ComplexTypeMapping>  
  19. </ResultMapping>  
  20. </FunctionImportMapping>  
  21. </EntityContainerMapping>  
  22. </Mapping>  
  23. </edmx:Mappings>  
  24. </edmx:Runtime>  

The difference between before and after for Result Mapping is shown in the following scrrens.


Step 7

Update your Entity Model as in the following to add the new ComplexTypes you used in the code above as in:

GetCustomerProductDetails_Result1, GetCustomerProductDetails_Result2


Step 8

Now we are done with the edmx changes. The last step is to call this procedure function.

 

  1. using (CustomerManagementEntitiesObj = newCustomerManagementEntities())    
  2. {  
  3.     var results = Obj.GetCustomerProductDetails(); //results is collection of customers  
  4.    var products = results.GetNextResult<GetCustomerProductDetails_Result2>(); //products is collection of products  
  5.  }  
In the source code of the web config just change the connection string before use.

Note: Use .Net Framework 4.5

That's it.

Thanks for reading….
 

Up Next
    Ebook Download
    View all
    milton
    Read by 0 people
    Download Now!
    Learn
    View all