Quantcast
Channel: Code and Programming » Search Results » table
Viewing all articles
Browse latest Browse all 10

Entity Framework has issues with composite key which is part auto generated

$
0
0

I’ve been searching for a while trying to figure this out. I’m trying to create a table with a composite primary key. The first part of the key is also a foreign key to the parent table. The second part is autogenerated on the SQL Server. So, I have a table that should look like this:

ParentId ChildId
——– ——-
1 1
1 2
1 3
2 1
2 2
2 3
2 4

The ChildId column is only unique within the context of the ParentId. The values are autogenerated on the server using an INSTEAD OF INSERT trigger so that each ChildId has its own sequence.

My issue is that while this works grand within SQL Server, and classic ADO.NET SqlCommand statements, Entity Framework does not want to work with this.

If I set the ChildId column’s StoreGeneratedPattern to be an Identity then EF generates SQL that looks like this:

insert [dbo].[ChildTable]([ParentId], [Name])
values (@0, @1)
select [ChildId]
from [dbo].[ChildTable]
where @@ROWCOUNT > 0 and [ParentId] = @0 and [Id] = scope_identity()

This just generates an error:

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException : 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.
—->
System.Data.OptimisticConcurrencyException : 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.

However, if I create a test table with a key based on a GUID and set the StoreGeneratedPattern to be an Identity then the SQL generated looks like this:

declare @generated_keys table([Id] uniqueidentifier)
insert [dbo].[GuidTable]([Name])
output inserted.[Id] into @generated_keys
values (@0)
select t.[Id]
from @generated_keys as g join [dbo].[GuidTable] as t on g.[Id] = t.[Id]
where @@ROWCOUNT > 0

And the entity in my application is updated with the value of the GUID that the SQL Server generated.

So, that suggests that the column does not have to be an IDENTITY column in order for the Entity Framework to get a value back, however, since it uses the logical table inserted the value of ChildId won’t be the value that it was changed to by the trigger. Also, the inserted table cannot have an UPDATE operation applied to it to push the values back inside the trigger (Tried that, it said “The logical tables INSERTED and DELETED cannot be updated.”)

I feel that I’ve kind of backed myself in to a corner here, but before I rethink the design is there any way to get the ChildId value(s) back into the application via Entity Framework?

Incoming search terms:

  • NSString csvRows unrecognized selector sent to instance

Viewing all articles
Browse latest Browse all 10

Trending Articles