Entity Framework inserting row with composite key

I am using Entity Framework 4.3.1 and I am trying to insert a new record into the table my ProductVersion entity is based on.

The ProductVersion entity has 2 properties that make up a composite primary key for the table called ProductId and ProductOrdinal.

Whenever someone updates a product entry I am creating and passing a ProductVersion entity back to my repository, incrementing the ProductOrdinal property, and attempting to add the entity to the context and save it.

I keep getting the following error:

The property 'ProductOrdinal' is part of the object's key information and cannot be modified.

Neither of the columns that make up the key are autonumbering and I have annotated the properties in my POCO with the following:

[Key, Column("PROD_Ordinal", Order=2), DatabaseGenerated(DatabaseGeneratedOption.None)]
public long ProductOrdinal { get; set; }

Answers


Marc_S was exactly right in his comment as to why this was not working.

From the exception and your description it seems that you are incrementing the 'ProductOrdinal' property on the existing 'ProductVersion' instance and trying to save it. What you probably want to do is create a new instance of 'ProductVersion' from the existing instance and increment the 'ProductOrdinal' property and save the new instance.


Need Your Help

How to remove this warning to this php script

php

I have this php query, which join many table from (baby class up to standard seven), so that I may get the Total SUM form SUM of each table.

How to make Checklist Items on Mediawiki?

checkbox mediawiki

Hello I am using a Mediawiki install on my dev server, just to organize my thoughts, write documentation for my apps, make note files/pages for general topics for easy reference, and so much more. A