Question

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; }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top