title: In Entity Framework, getting the value of an identity column after inserting
body: <p>I'm using EF4. I want to insert a new MyObject into the database. MyObject has two fields: </p>
  
  <p>Id:   int (Identity) and
  Name: string</p>
  
  <p>As I've seen in documentation Entity Framework is supposed to set MyObject.Id to the value generated by database after the call to SaveChanges() but in my case that doesn't happen.</p>
  
  <pre><code>using (var context = new MyEntities())
  {
      var myObject = MyObjects.CreateMyObject(0, "something"); // The first parameter is identity "Id"
      context.MyObjects.AddObject(myObject);
      context.SaveChanges();
      return myObject.Id; // The returned value is 0
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>This happens in one of my entities and others work fine. By the way, I checked and the DB column is identity and StoreGeneratedPattern is set to Identity.
  Here is the SSDL. I don't see any difference. The first one isn't working right:</p>
  
  <pre><code>    &lt;EntityType Name="OrgUnit"&gt;
        &lt;Key&gt;
          &lt;PropertyRef Name="Srl" /&gt;
        &lt;/Key&gt;
        &lt;Property Name="Srl" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /&gt;
        &lt;Property Name="TypeId" Type="smallint" Nullable="false" /&gt;
        &lt;Property Name="Name" Type="varchar" Nullable="false" MaxLength="80" /&gt;      
      &lt;/EntityType&gt;
  
      &lt;EntityType Name="OrgType"&gt;
        &lt;Key&gt;
          &lt;PropertyRef Name="Srl" /&gt;
        &lt;/Key&gt;
        &lt;Property Name="Srl" Type="smallint" Nullable="false" StoreGeneratedPattern="Identity" /&gt;
        &lt;Property Name="Title" Type="varchar" Nullable="false" MaxLength="120" /&gt;
        &lt;Property Name="Options" Type="int" Nullable="false" /&gt;
      &lt;/EntityType&gt;
  </code></pre>
  
  <p>The update is done successfully in the database and the identity is generated but the entity object is not updated with the new identity.</p>