C# 首先从Ef代码中的存储过程返回自定义表
是否可以从SP返回自定义表。我使用EF代码优先的方法 这是我的SPC# 首先从Ef代码中的存储过程返回自定义表,c#,asp.net,sql-server,asp.net-mvc,entity-framework,C#,Asp.net,Sql Server,Asp.net Mvc,Entity Framework,是否可以从SP返回自定义表。我使用EF代码优先的方法 这是我的SP CREATE PROCEDURE [dbo].[GetUserNotification] @ToUserId INT AS BEGIN select TU.FullName as ToUserName, FU.FullName as FromUserName, N.NotificationClickType, N.TemplateId, J.Titl
CREATE PROCEDURE [dbo].[GetUserNotification]
@ToUserId INT
AS
BEGIN
select TU.FullName as ToUserName,
FU.FullName as FromUserName,
N.NotificationClickType,
N.TemplateId,
J.Title as JobTitle,
J.Identifier as JobIdentifier,
B.Identifier as BidIdentifier
from Notification N
inner Join AppUser TU on TU.Identifier = N.ToUserId
inner Join AppUser FU on FU.Identifier = N.FromuserId
inner Join Bid B on B.Identifier = N.NotificationBidId
inner Join Job J on J.Identifier = B.JobId
where N.ToUserId=@ToUserId
Order By N.Identifier DESC
END
我的自定义视图模型
public class NotificationModel
{
public string ToUserName { get; set; }
public string FromUserName { get; set; }
public NotificationClickType NotificationClickType { get; set; }
public int TemplateId { get; set; }
public string JobTitle { get; set; }
public int JobIdentifier { get; set; }
public int BidIdentifier { get; set; }
}
我创建了相同的ViewModel。但是,在我使用SP看到的每个地方,我只能返回我在DbContext类中添加的单个表数据 将存储过程添加到EF模型时,它将自动创建复杂类型。(至少对我来说是这样,首先使用模型) 复杂类型的名称是添加了“\u Result”的SP名称,例如GetUserNotification\u Result 您可以使用复杂类型,如:
using (Entities dbContext = new Entities())
{
List<GetUserNotification_Result > data = dbContext.GetUserNotification(userId).ToList();
}
使用(Entities dbContext=new Entities())
{
List data=dbContext.GetUserNotification(userId.ToList();
}
或者,先演示如何使用代码。也可能有帮助。但我先使用代码。我根本没有edmx文件。如何将我的SP添加到我的上下文类。