Database Reference
In-Depth Information
To create and use a stored procedure that returns these entities, do the following.
1.
In your database, create the stored procedure in Listing 10-15.
Listing 10-15. The GetAllMedia Stored Procedure That Returns a Rowset with a Discriminator Column
create procedure [Chapter10].[GetAllMedia]
as
begin
select m.MediaId,c.Title,m.PublicationDate, null PlayTime,'Magazine' MediaType
from chapter10.Media c join chapter10.Magazine m on c.MediaId = m.MediaId
union
select d.MediaId,c.Title,null,d.PlayTime,'DVD'
from chapter10.Media c join chapter10.DVD d on c.MediaId = d.MediaId
end
2.
Right-click the design surface, and select Update Model from Database. Select the
GetAllMedia stored procedure. Click Finish to add the stored procedure to the model.
3.
Right-click the design surface, and select Add Function Import. In the dialog box, select
the GetAllMedia stored procedure. Enter GetAllMedia in the Function Import Name text
box. Select Entities as the type of collection and Media as the type of entity returned. Click
OK. This will create the skeleton <FunctionImportMapping> .
Right-click the .edmx file, and select Open With XML Editor. Edit the
<FunctionImportMapping> tag in the mapping section of the .edmx file to match the
code in Listing 10-16. This maps the rows returned by the stored procedure either to the
Magazine or to the DVD entity based on the MediaType column.
4.
Listing 10-16. This FunctionImportMapping Conditionally Maps the Returned Rows to Either the
Magazine or the DVD Entity.
<FunctionImportMapping FunctionImportName="GetAllMedia"
FunctionName="EF6RecipesModel.Store.GetAllMedia">
<ResultMapping>
<EntityTypeMapping TypeName="EF6RecipesModel.Magazine">
<ScalarProperty ColumnName="PublicationDate" Name="PublicationDate"/>
<Condition ColumnName="MediaType" Value="Magazine"/>
</EntityTypeMapping>
<EntityTypeMapping TypeName="EF6RecipesModel.DVD">
<ScalarProperty ColumnName="PlayTime" Name="PlayTime"/>
<Condition ColumnName="MediaType" Value="DVD"/>
</EntityTypeMapping>
</ResultMapping>
</FunctionImportMapping>
Follow the pattern in Listing 10-17 to use the GetAllMedia stored procedure via the
GetAllMedia() method.
5.
 
Search WWH ::




Custom Search