代码之家  ›  专栏  ›  技术社区  ›  jhunter

实体框架存储过程不可用

  •  3
  • jhunter  · 技术社区  · 16 年前

    我在edmx文件中添加了对存储过程的引用,然后右键单击它并选择“创建函数导入”,它被添加到模型浏览器中EntityContainer下的Function Imports文件夹中。

    据我所知,我应该可以这样使用它:

    sampleEntities db = new sampleEntities();
    db.SampleStoredProcedure();
    

    但它不会显示在db对象上。我是否错过了一步?函数Import设置为public,没有返回值,只有一个参数在展开时可以看到。

    2 回复  |  直到 16 年前
        1
  •  7
  •   Andrew Hare    16 年前

    存储过程是否返回一个简单的 )价值?如果是,设计师 will not generate the code for you :

    如果返回类型设置为简单 函数导入。

    但是,这个问题已经解决了 in the newest version of the Entity Framework :

    可以选择“无”和“标量” 像以前一样返回类型。 但是,当函数导入 创建时,会注入一些新代码 在模型代码后面 对ObjectContext的操作 它本身

        2
  •  3
  •   avalla    14 年前

    public bool ExecuteCMD(string Command) 
    {
        using (var entities = new DbEntities())
        {
            var conn = new System.Data.EntityClient.EntityConnection(entities.Connection.ConnectionString);
            try
            {
                conn.Open();
                using (EntityCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "DbEntities." + Command;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.ExecuteNonQuery();
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
    
    推荐文章