使用EF ExecuteStoreCommand批量插入无法正常工作,有什么问题?

时间:2010-12-24 14:00:34

标签: entity-framework bulkinsert

我正在使用SQL 2008和EF 我有以下存储过程为批量插入

CREATE Type [dbo].[xxx] as Table (
[ErrorCode] [nvarchar](10),
[ErrorMessage] [nvarchar](300),
[FieldName] [nvarchar](50),
[FieldLable] [nvarchar](300),
)
CREATE procedure dbo.InsertAll(@Records xxx READONLY)
as
begin

insert into dbo.MyTable
    select * from @Records;
end;
go

我传递一个Datatable作为参数(Type = structured),它有多个记录 使用SQLCommand.ExecuteNonQuery调用此proc时,但在使用contextObject.ExecuteStoreCommand调用时不执行任何操作。返回值=受影响的行始终为0

什么错了? EF不支持此类程序吗?我甚至没有得到任何例外:(

更新:运行SQL跟踪后,刚刚意识到正在生成的SQL语句的差异

使用contextObject.ExecuteStoreCommand

declare @p3 dbo.xxx
insert into @p3 values(N'M',N'ErrorMsg - 0',NULL,NULL)
insert into @p3 values(N'M',N'ErrorMsg - 1',NULL,NULL)
insert into @p3 values(N'M',N'ErrorMsg - 2',NULL,NULL)
exec sp_executesql N'InsertAll',N'@Records [xxx]
READONLY',@Records=@p3

使用SQLCommand.ExecuteNonQuery

declare @p1 dbo.xxx
insert into @p1 values(N'M',N'ErrorMsg - 0',NULL,NULL)
insert into @p1 values(N'M',N'ErrorMsg - 1',NULL,NULL)
insert into @p1 values(N'M',N'ErrorMsg - 2',NULL,NULL)
exec InsertAll @Records=@p1

如何让contextObject.ExecuteStoreCommand执行与SQLCommand.ExecuteNonQuery相同的SQL stmt?

1 个答案:

答案 0 :(得分:1)

我发现当实体框架根本无法满足我的目的时,我必须提供扩展方法。好的部分是为Context对象添加扩展通常会使您不必深入了解web.config或app.config以获取连接字符串。然后参数和返回值可以是通用的。我个人已经看到了很多使用这种策略的雄辩解决方案。

相关问题