Friday, April 19, 2013

Operation is not valid due to the current state of the object - LinQ to Entities - table without a primary key

You get this error when trying to insert into a table without a primary key. If you open the edmx file in XML view and take a look into the EntitySet it will be having a DefiningQuery tag (see below).

<EntitySet Name="Table1" EntityType="Model2.Store.Table1" store:Type="Tables" store:Schema="TABDATA" store:Name="Table1">
<DefiningQuery>
 SELECT "Table1"."Column1" as "Column1",
"Table1"."Column2" as "Column2",
"Table1"."Column3" as "Column3",
"Table1"."Column4" as "Column4",
"Table1"."Column5" as "Column5",
"Table1"."Column6" as "Column6",
"Table1"."Column7" as "Column7"
from "TABDATA"."Table1" "Table1"
</DefiningQuery>
</EntitySet>

The solution is to remove the DefiningQuery tag for that EntitySet as below:

<EntitySet Name="Table1" EntityType="Model2.Store.Table1" store:Type="Tables" store:Schema="TABDATA" store:Name="Table1">
</EntitySet>

You can insert new records now without any error.

No comments:

Post a Comment