简体   繁体   English

实体框架函数返回一个int而不是一个List

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

I have added a function in Entity Framework and I am trying to understand why it wants to return an int instead of a List<string> . 我在实体框架中添加了一个函数,并且试图理解为什么它要返回一个int而不是List<string>

I added the function to entity framework without an issue and once added and Validated the Context file looked as below: 我将函数添加到实体框架中没有任何问题,一旦添加并验证了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");
    }
}

I am calling the function below : 我正在调用以下函数:

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

and here is the function that I added: 这是我添加的功能:

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;

Does anyone know why Entity Framework would look for an int to come back instead of a List<string> 有谁知道为什么实体框架会寻找一个int而不是List<string>

EDIT: The solution mentioned in the possible duplicate does not work because it involves T-SQL. 编辑:可能重复中提到的解决方案不起作用,因为它涉及T-SQL。 This is PL/SQL and there is no equivalent to SET NOCOUNT ON when used in a function. 这是PL / SQL,在函数中使用时不等同于SET NOCOUNT ON

I found the answer... 我找到了答案...

I ended up changing: 我最终改变了:

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

to this: 对此:

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

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

        }
    }

Here is the link that I used to find the answer : 这是我用来找到答案的链接:

EDIT: While the above worked for me, I found a better way to solve the problem above this morning. 编辑:虽然以上对我有用,但我今天早上发现了一种更好的方法来解决问题。

Double click the edmx file and Right Click the open space of the model designer and go into the model browser. 双击edmx文件,然后右键单击模型设计器的开放空间,然后进入模型浏览器。

Right Click on the function that you are having a problem with and Click on Properties. 右键单击您遇到问题的函数,然后单击“属性”。

There is a option called Return Type to set what is being handed back... 有一个称为“返回类型”的选项可以设置要退回的内容...

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

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