0

我编写了以下生成报告的 sql。我运行一些 sql sueries 并将它们的结果存储在临时表中,并在存储过程结束时将所有表合并在一起。但是,当我将此存储过程添加到 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
4

1 回答 1

0

EF 不会为您创建复杂类型,但 EF 可以实例化声明复杂类型的对象集合。

例如

public class AType {
     public String Prop1 {get; set;}
}

context.Database.SqlQuery<AType>("select c1 as Prop1 from ATable");

将返回AType. 如果您将查询替换为对存储过程的调用,则相同。

在你的情况下,你必须声明一个类型来映射你的存储过程的返回。

否则,您可能会环顾四周,

  • 不知道EF能不能用
  • 您将失去使用编译语言的优势。

----- 版本

经过一些动态测试后,返回了正确的数字或​​对象,但我对它们无能为力。

于 2013-04-24T13:51:55.597 回答