Entity Framework – SaveChanges() doesn’t get the generated Identity key
Posted by Rajesh on December 24, 2010
Step to get identity key
Suppose T_Emp is table placed on emp.edmx. Now open with emp.edmx in xml and find EntityType Name=”T_EMP”.
Finally , what you have to do add StoreGeneratedPattern=”Identity” in primary key . Now Save the edmx and execute the project.
Now you can get identity key after SaveChanges()
Entities e1 = new Entities();
T_Empl a = new T_Empl();
a.Name = “Rajesh”;
e1.AddToT_Empl(a);
e1.SaveChanges();
var id = a.ID;
——————————————————
<EntityType Name=”T_EMP”>
<Key>
<PropertyRef Name=”ID” />
</Key>
<Property Name=”ID” Type=”number” Nullable=”false” StoreGeneratedPattern=”Identity”/>
<Property Name=”Name” Type=”varchar2″ MaxLength=”100″ />
</EntityType>
Advertisement