簡體   English   中英

從Ef Code First中的存儲過程返回自定義表

[英]Return custom table from Stored Procedure In Ef Code First

是否可以從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.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模型時,它將自動創建復雜類型。 (至少它是為我做的,使用Model First)。

復雜類型的名稱是添加了“_Result”的SP名稱,例如GetUserNotification_Result。

您可以使用復雜類型,如:

using (Entities dbContext = new Entities())
{
    List<GetUserNotification_Result > data = dbContext.GetUserNotification(userId).ToList();
}

或者, https://msdn.microsoft.com/en-us/data/jj691402.aspx顯示如何首先使用代碼。 如何在實體框架6(代碼優先)中調用存儲過程? 也可能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM