例外:使用Entity框架ExecuteStoreQuery函数执行Insert Stored Procedure

时间:2014-12-10 12:52:57

标签: c# .net entity-framework stored-procedures entity-framework-5

我正在尝试使用Entity Framework ExecuteStoreQuery通过存储过程插入数据。我的过程返回标识作为输出参数。

我的存储过程:

CREATE  PROCEDURE [dbo].[Insert_Department]
    @Department NVARCHAR(100),
    @new_identity INT OUTPUT        
AS 
BEGIN 
   SET NOCOUNT ON

   INSERT INTO [Department](Department)
   VALUES (@Department)

   SELECT @new_identity = SCOPE_IDENTITY()
   SELECT @new_identity AS Id
   RETURN   
END

来源

class Program
{
        static void Main(string[] args)
        {
            using (var initializeDbContext=new InitializeDbContext())
            {
                var name = new object[] { new SqlParameter("@Department", "Bob") };
                var identity = new object[] { new SqlParameter("@new_identity", "Bob") };
                initializeDbContext.AddorUpdate("exec [Insert_Department] @Department,@new_identity", name,identity);
            }
        }
    }

    public class InitializeDbContext : DbContext
    {
        public InitializeDbContext()
            : base("name=CreditRatingDbEntities")
        {
            Database.SetInitializer<InitializeDbContext>(null);
        }

        public ObjectResult<int?> AddorUpdate(string procedureName, params object[] parameters)
        {
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<int?>(procedureName, parameters);
        }
    }

当我调用AddOrUpdate函数时,我得到了异常

  

从对象类型System.Object []到已知的托管提供程序本机类型

,不存在任何映射

我该如何解决这个问题?

当我在存储过程中返回ObjectResult作为输出参数时,我必须在identity中放入什么返回类型?

0 个答案:

没有答案