After Insert and SaveChanges getting old identity
<p>I have a table with an <code>Id</code> column which is set to <code>Identity</code>. Now, when I am inserting record in a loop i.e.</p>

<pre><code>foreach (int i in array)
{
    using (var entities = new myEntities())
    {
            TableA obj = new TableA();
            obj.Name = "MyName";
            obj.Address = "MyAddress";
            entities.TableA.Add(obj);
            isAdded = entities.SaveChanges() &gt; 0;

            if (isAdded)
            {
                 int id = obj.Id;
                 obj.IdInString = id.ToString();
                 entities.SaveChanges();
            }
    }
}
</code></pre>

<p>Now, for the first iteration it brings correct Id. But for second iteration it still brings back the id of first row inserted. Whereas I can see in the DB that second row is inserted and ID is incremented too.</p>

<p>What is going on? What am I doing wrong here?
EDIT:
I came back to this issue after some time and found that this is NOT to do with loop. It is returning old ID even when I come to the page next time. It seems it is getting cached. I have already used entity inside "using" block which means it is getting destroyed. 
Please note this is used in ASP.Net web application
Thanks..</p>