Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
EntityFramework和sp_executesql_Sql_Entity Framework - Fatal编程技术网

EntityFramework和sp_executesql

EntityFramework和sp_executesql,sql,entity-framework,Sql,Entity Framework,为什么从Entity Framework ObjectContext.ExecuteStoreCommand()运行以下自动生成的SQL时,所有参数都会导致NULL exec sp_executesql N'SaveModel', N'@ModelID int,@Name nvarchar(24),@Description nvarchar(34)', @ModelID=4, @Name=N'Status', @Description=N'The status of a model.' 在E

为什么从Entity Framework ObjectContext.ExecuteStoreCommand()运行以下自动生成的SQL时,所有参数都会导致NULL

exec sp_executesql 
N'SaveModel',
N'@ModelID int,@Name nvarchar(24),@Description nvarchar(34)',
@ModelID=4,
@Name=N'Status',
@Description=N'The status of a model.'
在ExecuteStoreCommand运行之后,我从探查器中获取了这个SQL,您可以看到@ModelID参数被设置为4,Name='Status',Description='thestatusofthemodel'。但是,在存储过程SaveModel中打印ModelID时,它为NULL。以下是演示空参数的存储过程:

USE [Bluewater]
GO
/****** Object:  StoredProcedure [dbo].[SaveModel]    Script Date: 08/09/2011 13:15:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Nicholas Barger
-- Create date: 08/07/2011
-- Description: Save model entity (create/update).
-- =============================================
ALTER PROCEDURE [dbo].[SaveModel] 
@ModelID int = null, 
@Name varchar(255) = null,
@Description varchar(1000) = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

PRINT ('ModelID: ' + CAST(ISNULL(@ModelID, 0) AS VARCHAR(10)));

END
--张贴答案--

问题是最初使用ExecuteStoreCommand()调用时,我认为第一个参数只请求存储过程的名称,而不是完整的存储过程语法。下面是一个断开代码的示例:

e.ExecuteStoreCommand("SaveModel", new SqlParameter[] { 
            new SqlParameter("ModelID", model.ModelID), 
            new SqlParameter("Name", model.Name),
            new SqlParameter("Description", model.Description)
})
以下是工作代码:

e.ExecuteStoreCommand("SaveModel @ModelID, @Name, @Description", new SqlParameter[] { 
            new SqlParameter("ModelID", model.ModelID), 
            new SqlParameter("Name", model.Name),
            new SqlParameter("Description", model.Description)
})

在那里生成的sql不正确,下面的sql生成您期望的结果

exec sp_executesql 
N'SaveModel @ModelID, @Name, @Description',
N'@ModelID int,@Name varchar(24),@Description varchar(34)',
@ModelID=4,
@Name=N'Status',
@Description=N'The status of a model.'
在我看来,这也是

A) 实体框架中的一个bug


B) 您的实体模型有问题

在实体模型中生成的sql不正确,以下sql将生成您期望的结果

exec sp_executesql 
N'SaveModel @ModelID, @Name, @Description',
N'@ModelID int,@Name varchar(24),@Description varchar(34)',
@ModelID=4,
@Name=N'Status',
@Description=N'The status of a model.'
在我看来,这也是

A) 实体框架中的一个bug

B) 实体模型存在问题