0

I am using Entity Framework 4 self-tracking entities template (STE) and SQL Server CE. I am seeing some very odd behavior. Everything works great if I just insert or update, delete. However if I insert an object and then update it an UPDATE and an INSERT SQL statement will be generated for the object when I perform the update (See below).

I am using a repository pattern and I dispose of the repository and the ObjectContext it wraps for each of the calls so the update uses a new ObjectContext. Also note that if I restart the server the update of the object works fine so it is only the case where I have inserted an object.

The update does the "usual" ApplyChanges and SaveChanges since I am using STE.

The trace for the initial insert is:

Creating ComputerEntities
--=============== BEGIN COMMAND ===============

declare @0 UniqueIdentifier set @0 = '2975bbbb-36a1-4c0a-8075-be273a96cd42'
declare @1 Bit set @1 = 'True'
declare @2 UniqueIdentifier set @2 = '2913bff8-9f2b-4965-8e02-5cb2a98f4498'
declare @3 Int set @3 = '2'
declare @4 Int set @4 = '1'
declare @5 NVarChar set @5 = 'test'
declare @6 NVarChar set @6 = 'C:\ProgramData\Kailana\ReVersion\Repositories\Projects\2975bbbb-36a1-4c0a-8075-be273a96cd42\2975bbbb-36a1-4c0a-8075-be273a96cd42'

insert [Repository]([Id], [Enabled], [ComputerId], [SQLVersionId], [ReleaseId], [Name], [Description], [DataSource], [Password], [DbUser], [DbUserPassword])
values (@0, @1, @2, @3, @4, @5, null, @6, null, null, null)

go

--=============== END COMMAND ===============
  • Disposing the repository
  • Disposing the repositories Context.

Next I do an update and this is the SQL that is generated:

Creating ComputerEntities
--=============== BEGIN COMMAND ===============

declare @0 Bit set @0 = 'True'
declare @1 UniqueIdentifier set @1 = '2913bff8-9f2b-4965-8e02-5cb2a98f4498'
declare @2 Int set @2 = '2'
declare @3 Int set @3 = '1'
declare @4 NVarChar set @4 = 'tested'
declare @5 NVarChar set @5 = 'C:\ProgramData\Kailana\ReVersion\Repositories\Projects\2975bbbb-36a1-4c0a-8075-be273a96cd42\2975bbbb-36a1-4c0a-8075-be273a96cd42'
declare @6 UniqueIdentifier set @6 = '2975bbbb-36a1-4c0a-8075-be273a96cd42'

update [Repository]
set [Enabled] = @0, [ComputerId] = @1, [SQLVersionId] = @2, [ReleaseId] = @3, [Name] = @4, [Description] = null, [DataSource] = @5, [Password] = null, [DbUser] = null, [DbUserPassword] = null
where ([Id] = @6)
go

-=============== END COMMAND ===============
--=============== BEGIN COMMAND ===============

declare @0 UniqueIdentifier set @0 = '2975bbbb-36a1-4c0a-8075-be273a96cd42'
declare @1 Bit set @1 = 'True'
declare @2 UniqueIdentifier set @2 = '2913bff8-9f2b-4965-8e02-5cb2a98f4498'
declare @3 Int set @3 = '2'
declare @4 Int set @4 = '1'
declare @5 NVarChar set @5 = 'test'
declare @6 NVarChar set @6 = 'C:\ProgramData\Kailana\ReVersion\Repositories\Projects\2975bbbb-36a1-4c0a-8075-be273a96cd42\2975bbbb-36a1-4c0a-8075-be273a96cd42'

insert [Repository]([Id], [Enabled], [ComputerId], [SQLVersionId], [ReleaseId], [Name], [Description], [DataSource], [Password], [DbUser], [DbUserPassword])
values (@0, @1, @2, @3, @4, @5, null, @6, null, null, null)
go

--=============== END COMMAND ===============

I have exhausted every thought I have on what is causing this so any idea that you have that can get me to look in a new direction is appreciated.

Thanks, Rick

4

1 回答 1

0

成功插入实体后,尝试在实体上调用AcceptChanges或。MarkAsUnchanged

于 2012-06-19T08:38:19.170 回答