InsertOnSubmit(entity) - Composite Keys

Aug 15, 2011 at 9:01 PM

I have Entity A that contains child entity B which contains child entity C. Child entity C does not have a unqiue key. Rather it uses a composite key consisting of entity B's unique key and a languagekey. When I create a new entity of type A that contains two entities of type B each containing any number of entity C I receive an error message (see below). Is there something special I need to do with composite keys?

Visual Studio 2010
LinqConnect 2.50.25.0
dotConnect 6.30.172
Code the errors:
targetDataContext.GetTable(entity.GetEntityType()).InsertOnSubmit(entity);

Error:
Can not insert entity with the same key if key is not database generated.

Coordinator
Aug 16, 2011 at 2:10 AM

Hi there,

Are you generating the keys yourself or leaving it up to the database to generate them? 

Cheers

Matt.

Aug 16, 2011 at 3:18 AM
The unique keys for entity A and B are generated by the db using a sequence generator. The composite key for entity C uses entity B's unique key and a languagekey which is not database generated. The orm generated partial class decorates the properties on entity C with a primarykey attribute. Perhaps I need to do more. Not sure. Thanks for responding.

On Mon, Aug 15, 2011 at 9:10 PM, mhunter <notifications@codeplex.com> wrote:

From: mhunter

Hi there,

Are you generating the keys yourself or leaving it up to the database to generate them?

Cheers

Matt.

Read the full discussion online.

To add a post to this discussion, reply to this email (LINQ2SQLEB@discussions.codeplex.com)

To start a new discussion for this project, email LINQ2SQLEB@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Aug 16, 2011 at 3:30 AM

From memory, if you dealing with multiple entities of the same type, if you don't provide a key it will complain that you have a duplicate because more than one key is null, this check occurs before anything is sent to the database.  Try setting the key on the entities temporarily - then when you apply the changes your temporary keys will be replaced with keys generated by the database.

Aug 16, 2011 at 6:08 PM
I found a solution by opening the model in Entity Developer. I selected entity C's composite key. I clicked on the key generated from Entity B's unique key. In the properties, I switched Auto Generated Value from false to true. This appears to have fixed the issue I was having.

On Mon, Aug 15, 2011 at 10:30 PM, mhunter <notifications@codeplex.com> wrote:

From: mhunter

From memory, if you dealing with multiple entities of the same type, if you don't provide a key it will complain that you have a duplicate because more than one key is null, this check occurs before anything is sent to the database. Try setting the key on the entities temporarily - then when you apply the changes your temporary keys will be replaced with keys generated by the database.

Read the full discussion online.

To add a post to this discussion, reply to this email (LINQ2SQLEB@discussions.codeplex.com)

To start a new discussion for this project, email LINQ2SQLEB@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Aug 17, 2011 at 12:47 AM

Good to hear :)