0

when we call SaveChanges() from DbContext to insert record it will check scope_identity to get row affected and return it, every things is ok, but if we use with link server and synonyms because scope_identiy is local and scope_identiy will return null because it is remote, and it will say :

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException(occurred) Message=Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

becuse DbContext want to execute code like this after insert :

SELECT [Id]
FROM [dbo].[PortalUsers]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()

and scope_identity return null and exception will throwen

can we tell entity that dont check row affected, or something like this to solve problem?

Emran Sadeghi
  • 612
  • 6
  • 20
  • http://stackoverflow.com/questions/6819813/solution-for-store-update-insert-or-delete-statement-affected-an-unexpected-n ? – romanoza May 03 '15 at 07:31
  • thanks, but link that you sent is not my answer – Emran Sadeghi May 03 '15 at 08:40
  • @EmranSadeghi you could create 3 stored procedures for CRUD and use Store Procedure Mapping to map these procedures with the crud operations of your entity. Inside The "insert" store procedure you can retrieve manually the identity from the remote server (which will take a little effort cause you can not use neither OUTPUT but you can call a stored procedure on the remote server that will do the work!). – Stavros Zotalis May 03 '15 at 10:36
  • @Blim no i dont want create stored procedure, i want use entity with liked server as like as normal connection string! – Emran Sadeghi May 03 '15 at 15:17

0 Answers0