繁体   English   中英

如何从使用EntityFramework构建的C#项目中的ComplexType Sql Server存储过程中检索数据

[英]How to retrieve data from a ComplexType Sql Server stored procedure inside C# project built with EntityFramework

我编写了一个SQL Server存储过程,它通过在多个表上使用内部连接来返回数据。 这意味着它没有返回任何特定的实体。

这是我的程序:

CREATE proc usp_viewlog_query

as
SET FMTONLY OFF

declare @start_date datetime
set @start_date = DATEADD(day, -30, GETDATE())

select distinct v.VehilceId, v.Year, vs.StatusName, vp.PhotoName,
sp.StateName, v.Price, v.ManufacturerId, v.Model, v.UrlAlias into #t from Vehicle v
inner join (
    select distinct  top 10 VehicleId, ViewedOn from ViewLog where ViewedOn >= @start_date --order by ViewedOn desc
) vl on v.VehilceId = vl.VehicleId
inner join StateParish sp on v.StateCode = sp.StateCode
inner join VehiclePosition vs on v.StatusCode = vs.StatusCode
left join (select top 1 vehicleid, photoname from VehiclePhoto) vp on v.VehilceId = vp.VehicleId

update #t set PhotoName = (select top 1 photoname from VehiclePhoto where VehicleId = #t.VehilceId)

select
VehilceId as VehicleId, [year], StatusName, 
PhotoName, StateName, Price, ManufacturerId,
Model, UrlAlias
from #t
drop table #t

我使用Update Model From Database添加了上述过程,然后将该函数作为ComplexType导入。

但是,如何从上面的dataset获取记录到我的项目中并访问它? 我已经搜索了一段时间,但我得到的是如何导入程序,我已经完成了。

我不知道访问上述函数的代码并获取项目中的数据。

首先创建一个具有与过程列名相同的属性名称的类型,例如: -

public class MyComplexType
{
    public int VehilceId { get; set; }
    public string StatusName { get; set; }
    public string PhotoName { get; set; }
    //other properties
}

然后打电话给你的程序: -

using(var context = new DatabaseContext())
{
    var result = context.Database.SqlQuery<MyComplexType>("usp_viewlog_query").ToList();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM