I have an entity called Tree, which has a primary key called Id (with StoreGeneratedPattern = Identity) that when using the following code, correctly inserts into the database.
using(TestContainer tss = new TestContainer())
{
Tree tree = new Tree()
{
Name = "TestTree"
};
tss.Trees.AddObject(tree);
tss.SaveChanges();
}
I have a backing sequence + trigger to handle the auto incremented primary key Id. I have verified that this actually inserts into the database correctly.
Calling tss.Refresh(System.Data.Objects.RefreshMode.StoreWins, tree);
does not update the object (the 'Id' field is still 0). Any ideas?
<EntityType Name="Tree">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Name" Nullable="false" />
<NavigationProperty Name="HeuristicCulls" Relationship="TOPSSSimpleSelect.TreeHeuristicCull" FromRole="Tree" ToRole="HeuristicCull" />
<Property Type="Int32" Name="Type" Nullable="false" />
<NavigationProperty Name="PR_T" Relationship="TOPSSSimpleSelect.PR_TTree" FromRole="Tree" ToRole="PR_T" />
<NavigationProperty Name="TreeItems" Relationship="TOPSSSimpleSelect.TreeTreeItem" FromRole="Tree" ToRole="TreeItem" />
<Property Type="Byte" Name="IsRoot" Nullable="false" />
<Property Type="Byte" Name="IsProductRoot" Nullable="false" />
<NavigationProperty Name="TreeProducts" Relationship="TOPSSSimpleSelect.T_TP" FromRole="Tree" ToRole="TreeProducts" />
</EntityType>
<EntityType Name="TreeItem">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<NavigationProperty Name="Questions" Relationship="TOPSSSimpleSelect.TI_Q" FromRole="TreeItem" ToRole="Question" />
<Property Type="String" Name="Name" Nullable="false" />
<NavigationProperty Name="SubmitRules" Relationship="TOPSSSimpleSelect.SubmitRuleTreeItem" FromRole="TreeItem" ToRole="SubmitRule" />
<NavigationProperty Name="PR_TI" Relationship="TOPSSSimpleSelect.PR_TITreeItem" FromRole="TreeItem" ToRole="PR_TI" />
<NavigationProperty Name="Tree" Relationship="TOPSSSimpleSelect.TreeTreeItem" FromRole="TreeItem" ToRole="Tree" />
<Property Type="Int32" Name="TreeId" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="Byte" Name="IsRoot" Nullable="false" />
</EntityType>
Open up the .edmx file with the XML editor and look for the section that begins with the following line:
Below should be an EntityType tag and in it is a definition of the database table. Make sure that the property for your ID column has StoreGeneratedPattern="Identity" in it.
Below this SSDL section is a CSDL section that looks similar, but defines the C# object that represents this entity. The visual designer only seems to fill in the StoreGeneratedPatternin this section, but not the SSDL section.
EDIT WITH SAMPLE
Here's a sample EDMX file for an Employee entity, with nothing but an ID, FirstName, and LastName property. The ID is the field you want auto generated by the database. Note that there are two different places where StoreGeneratedPattern is needed.