【发布时间】: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