簡體   English   中英

實體框架函數返回一個int而不是一個List

[英]Entity Framework function returning an int instead of a List

我在實體框架中添加了一個函數,並且試圖理解為什么它要返回一個int而不是List<string>

我將函數添加到實體框架中沒有任何問題,一旦添加並驗證了Context文件,如下所示:

public partial class Entities : DbContext
{
    public Entities()
        : base("name=Entities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<AppName> AppNames { get; set; }
    public DbSet<AppStatus> AppStatus { get; set; }
    public DbSet<Audit> Audits { get; set; }
    public DbSet<EntryLog> EntryLogs { get; set; }
    public DbSet<LogType> LogTypes { get; set; }
    public DbSet<ModuleName> ModuleNames { get; set; }
    public DbSet<Trace> Traces { get; set; }
    public DbSet<Error> Errors { get; set; }

    public virtual int GET_ALL_APPS()
    {
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("GET_ALL_APPS");
    }
}

我正在調用以下函數:

public List<string> GetApplicationNames()
    {
        using (ComData.Entities db = new ComData.Entities())
        {
            return db.GET_ALL_APPS();                 
        }
    }

這是我添加的功能:

create or replace FUNCTION GET_ALL_APPS RETURN SYS_REFCURSOR
  AS 
    PO_RESULT SYS_REFCURSOR;
  BEGIN
    OPEN PO_RESULT FOR
        SELECT UNIQUE 
          APP_NAME
        FROM 
          LG_ENTRY_BASE_LOG;
      RETURN PO_RESULT;
    END;

有誰知道為什么實體框架會尋找一個int而不是List<string>

編輯:可能重復中提到的解決方案不起作用,因為它涉及T-SQL。 這是PL / SQL,在函數中使用時不等同於SET NOCOUNT ON

我找到了答案...

我最終改變了:

public List<string> GetApplicationNames()
{
    using (ComData.Entities db = new ComData.Entities())
    {
        return db.GET_ALL_APPS();                 
    }
}

對此:

public IQueryable<List<string>> GetApplicationNames()
    {
        using (ComData.Entities db = new ComData.Entities())
        {

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<List<string>>("GET_ALL_APPS").ToList().AsQueryable();

        }
    }

這是我用來找到答案的鏈接:

編輯:雖然以上對我有用,但我今天早上發現了一種更好的方法來解決問題。

雙擊edmx文件,然后右鍵單擊模型設計器的開放空間,然后進入模型瀏覽器。

右鍵單擊您遇到問題的函數,然后單擊“屬性”。

有一個稱為“返回類型”的選項可以設置要退回的內容...

暫無
暫無

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

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