我编写了以下用于生成报告的sql。我运行一些sql sueries并将其结果存储在临时表中,并在sproc的末尾将所有表联合起来。然而,当我将这个sproc添加到EF时,它不会为它创建一个复杂的类型,没有它就没用。如何使EF识别返回的结果集并创建复杂类型。
ALTER PROC [dbo].[Report_AllMediaDetails]
(
@profileID AS INT,
@organisationID as INT,
@startDate as date,
@endDate as date
)
AS
create table #Temp_ReportData
(
ItemOrgID int,
MediaName char(100),
CountryName char(100),
ItemOverRideDate DateTime,
Rating int,
BatchNo int,
NoInBatch int,
BatchDate DateTime
)
insert into #Temp_ReportData
select
oi.ID,
mc.Name as MediaName,
lcc.Name as Country,
i.OverrideDate as Date,
oi.Rating,
b.ID BatchNo,
i.NumInBatch ItemNumberInBatch,
bah.ChangedAtUtc as BatchDate
from
ProfileResults PR
INNER JOIN Items i ON PR.ItemID = I.ID
inner join Batches b on b.ID=i.BatchID
inner join ItemOrganisations oi on i.ID=oi.ItemID
inner join Lookup_MediaChannels mc on i.MediaChannelID=mc.id
left outer join Lookup_Cities lc on lc.ID=mc.CityID
left outer join Lookup_Countries lcc on lcc.ID=mc.CountryID
inner join BatchActionHistory bah on b.ID=bah.batchid
where pr.ProfileID = @profileID
and oi.OrganisationID = @organisationID
and b.StatusID IN (6,7)
and bah.BatchActionID = 6
and i.StatusID = 2
and i.IsRelevant = 1
and i.OverrideDate between @startDate and @endDate
--Create a temp table to hold the B section ByLines
create table #Temp_ReportByLines
(
ItemOrgID int,
ByLine char(100),
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
ItemOverRideDate date
)
insert into #Temp_ReportByLines
select
oi.ID,
bl1.FirstName +' '+ bl1.Surname as ByLine,
b.ID,
i.NumInBatch,
bah.ChangedAtUtc,
oi.rating,
i.OverrideDate
from ProfileResults PR
INNER JOIN Items i ON PR.ItemID = I.ID
inner join Batches b on b.ID=i.BatchID
inner join ItemOrganisations oi on i.ID=oi.ItemID
left outer join ItemByLines ib on ib.ItemID=i.ID
left outer join ByLines bl1 on bl1.ID=ib.ByLineID
inner join BatchActionHistory bah on b.ID=bah.batchid
where pr.ProfileID = @profileID
and oi.OrganisationID = @organisationID
and b.StatusID IN (6,7)
and bah.BatchActionID = 6
and i.StatusID = 2
and i.IsRelevant = 1
and bl1.IsByLine=1
and i.OverrideDate between @startDate and @endDate
--Create a temp table to hold the I section issues
create table #Temp_ReportIssues
(
ItemOrgID int,
IssueNo int,
Issue char(300),
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
ItemOverRideDate date
)
insert into #Temp_ReportIssues
select
iss.ItemOrganisationID,
pri.Code, pri.Name,
td.BatchNo,
td.NoInBatch,
td.BatchDate,
td.Rating,
td.ItemOverRideDate
from ItemOrganisationIssues iss
inner join #Temp_ReportData td on td.itemorgid= iss.ItemOrganisationID
inner join ProjectIssues pri on pri.ID=iss.IssueID
--Create a temp table to hold the M section messages
create table #Temp_ReportMessages
(
ItemOrgID int,
MessageType char(100),
MessageNo int,
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
itemOverRideDate date
)
insert into #Temp_ReportMessages
select
itemorgid, lmt.Name as MessageType ,pm.NeptuneMessageID as MessageNo,
td.BatchNo,
td.NoInBatch,
td.BatchDate,
td.Rating,
td.ItemOverRideDate
from #Temp_ReportData td
left outer join ItemOrganisationMessages iom on iom.ItemOrganisationID=td.ItemOrgID
left outer join ProjectMessages pm on iom.MessageID=pm.id
left outer join Lookup_MessageTypes lmt on lmt.ID=pm.MessageTypeID
--Create a temp table to hold the S section Source
create table #Temp_ReportSources
(
ItemOrgID int,
SourceFullName char(100),
SourceType char(20),
BatchNo int,
NumInBatch int,
BatchDate Date,
Rating int,
ItemOverRideDate date
)
insert into #Temp_ReportSources
select
ios.ItemOrganisationID,
bl2.FirstName+' '+bl2.Surname as SourceFullName,
lst.name as SourceTypeDesc,
td.BatchNo,
td.NoInBatch,
td.BatchDate,
td.Rating,
td.ItemOverRideDate
from ItemOrganisationSources ios
inner join #Temp_ReportData td on td.ItemOrgID = ios.ItemOrganisationID
inner join ByLines bl2 on bl2.ID=ios.ByLineID and bl2.IsByLine=0
left outer join Lookup_SourceTypes lst on lst.ID=ios.SourceTypeID
-- Union all the temp tables into a formatted resultset
select
td.MediaName,td.CountryName,td.ItemOverRideDate, td.Rating,td.BatchNo,td.NoInBatch, td.BatchDate, '' as DataType,'' as ByLine,'' as IssueNo,'' as Issue, '' MessageNo, '' as Message,'' as SourceName,'' as SourceType from #Temp_ReportData td
union select '','',tb.ItemOverRideDate,tb.rating,tb.batchno,tb.NumInBatch,tb.batchdate,'B', tb.ByLine,'','','','','','' from #Temp_ReportByLines tb
union select '','',tr.ItemOverRideDate,tr.rating,tr.batchno,tr.NumInBatch,tr.batchdate,'I','',tr.issueno,tr.issue,'','','', '' from #Temp_ReportIssues tr
union select '','',tm.ItemOverRideDate,tm.rating,tm.batchno,tm.NumInBatch,tm.batchdate,'M','','','',tm.messageno,tm.messagetype,'','' from #Temp_ReportMessages tm
union select '','',ts.ItemOverRideDate,ts.rating,ts.batchno,ts.NumInBatch,ts.batchdate,'S','','','','','',ts.sourcefullname,ts.sourcetype from #Temp_ReportSources ts
order by td.NoInBatch,td.BatchNo, datatype
答案 0 :(得分:0)
EF不会为您创建复杂类型,但EF可以实例化声明的复杂类型的对象集合。
例如
public class AType {
public String Prop1 {get; set;}
}
context.Database.SqlQuery<AType>("select c1 as Prop1 from ATable");
将返回AType
的集合。如果使用对存储过程的调用替换查询,则相同。
在这种情况下,您必须声明一个用于映射存储过程返回的类型。
否则你可以看看dynamic,但是:
----- EDITION
在使用dynamic进行一些测试后,会返回正确的数字或对象,但我对它们无能为力。