【问题标题】:Bulk insert with EF ExecuteStoreCommand not working, what is wrong?使用 EF ExecuteStoreCommand 批量插入不起作用,有什么问题?
【发布时间】:2010-12-24 14:00:34
【问题描述】:

我正在使用 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

我正在传递一个数据表作为具有多条记录的参数(类型=结构化) 此过程在使用 SQLCommand.ExecuteNonQuery 调用时有效,但在使用 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 语句?

【问题讨论】:

    标签: entity-framework bulkinsert


    【解决方案1】:

    我发现当实体框架根本无法满足我的要求时,有必要提供扩展方法。好的部分是向 Context 对象添加扩展通常会使您不必深入研究 web.config 或 app.config 以获取连接字符串。那么参数和返回值可以是通用的。我个人见过不少使用这种策略的雄辩的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      • 2013-07-13
      相关资源
      最近更新 更多