使用SQL 2008 R2 11月发行版数据库和.net 4.0 Beta 2 Azure辅助角色应用程序.worker角色收集数据并将其插入到具有一个标识列的单个SQL表中.因为可能有多个此工作者角色的实例在运行,所以我在SQL表上创建了一个Insert而不是触发器.触发器使用SQL Merge函数执行Upsert功能.使用T-SQL我能够正确验证插入而不是触发器功能,在更新现有行时插入新行.这是我的触发器的代码:
Create Trigger [dbo].[trgInsteadOfInsert] on [dbo].[Cars] Instead of Insert
as
begin
set nocount On
merge into Cars as Target
using inserted as Source
on Target.id=Source.id AND target.Manufactureid=source.Manufactureid
when matched then
update set Target.Model=Source.Model,
Target.NumDoors = Source.NumDoors,
Target.Description = Source.Description,
Target.LastUpdateTime = Source.LastUpdateTime,
Target.EngineSize = Source.EngineSize
when not matched then
INSERT ([Manufactureid]
,[Model]
,[NumDoors]
,[Description]
,[ID]
,[LastUpdateTime]
,[EngineSize])
VALUES
(Source.Manufactureid,
Source.Model,
Source.NumDoors,
Source.Description,
Source.ID,
Source.LastUpdateTime,
Source.EngineSize);
End
Run Code Online (Sandbox Code Playgroud)
在worker角色中,我使用Entity Framework作为对象模型.当我调用SaveChanges方法时,我收到以下异常:
OptimisticConcurrencyException
Store update, insert, …Run Code Online (Sandbox Code Playgroud) triggers entity-framework sql-server-2008-r2 .net-4.0-beta-2