2015-10-20 54 views
1

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

我添加的功能實體框架沒有問題,一旦添加和驗證上下文文件看起來如下:

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等效。

+1

我想這是因爲你宣佈它作爲'公共虛擬INT GET_ALL_APPS()' –

+0

可能重複[存儲過程返回int,而不是結果集](http://stackoverflow.com/questions/18245567/stored-procedure-returns-int-instead-of-result-set) – cubrr

+0

@SerhiyChupryk我相信這是由EF生成的。 – cubrr

回答

0

我找到了答案......

我最終改變:

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(); 

     } 
    } 

這裏是我用來尋找answer鏈接:

編輯:雖然上面的工作對我來說,我發現了一個更好的方法來解決問題abov今天早上。

雙擊edmx文件並右鍵單擊模型設計器的空白處並進入模型瀏覽器。

右鍵單擊您遇到問題的功能,然後單擊屬性。

有一個名爲返回類型設置什麼是被交還給選項...