Currently, I'm using the following approach:
- I created a view that returns no row with the exact structure as the stored procedure result -
View1
- in the stored procedure, I include Union Select from
View1
- in EF, I added the
View1
to model (to create entity from it) and map that entity to the result of the stored procedure
Here is the code:
Create View View1 as
select cast('' as nvarchar(20)) as fld1, cast(0 as int) as fld2
Create Procedure MyProc
@par1 as nvarchar(50)
As
select fld1, fld2 into #tmpTable from Table1
union
select fld1, fld2 from Table2
union
select fld1, fld2 from Table3
union
select fld1, fld2 from View1
-- select result
select distinct fld1, fld2 from #tmpTable
With this approach, I accomplish what I need:
- the stored procedure in EF returns a complex type, which gets updated when I update the model from DB
- the stored procedure and the complex result type don't get off-sync; that's because if by any chance the stored procedure gets changed, to return a different result set, and the related view is not updated accordingly, this will get caught as soon as the stored procedure is tested - because of the union join.