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

Python reading from tempfile not successful

python subprocess readfile temporary-files

with tempfile.NamedTemporaryFile(delete = False) as tmpfile:

Panorama navigation in Windows phone

windows-phone-7 navigation panorama-control

It's very quick question but I was looking for it in Google for 2 days without any result...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.