Dot Net Learning Zone Blog

A blog about to learn Dot Net

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.