title: How does entity framework keep track if the entity database’s values were changed
body: <p>I have the following repository method,which mainly do two things:-</p>
  
  <ol>
  <li><p>Create Technology record (Parent record), and generates a system generated ID for it.</p></li>
  <li><p>Then create a child record , which have its ID equal to the system generated TechnologyID, as follow:-</p>
  
  <pre><code>public void InsertOrUpdateServer(TServer server....)
  {
  
      if (server.TServerID == default(int))
      {
          // New entity
          var resource = entities.Resources.SingleOrDefault(a =&gt; a.RESOURCEID == assetid); 
          int technologyypeID = GetTechnologyTypeID("Server");
          Int64 max =GetTagMaximumeNumber(technologyypeID);
          Technology technology = new Technology
          {
  
              StartDate = DateTime.Now,
              //code goes here...
          };
  
          entities.Technologies.Add(technology);
          //code goes here
  
          server.TServerID= technology.TechnologyID;// This what makes me confused!!!!!
  
          entities.TServers.Add(server);
  
          technology.IsDeleted = false;
          technology.IsCompleted = true;
  
      }    
      entities.SaveChanges();
  </code></pre></li>
  </ol>
  
  <p>But how did EF get the technologyID  and assign it to the ServerID, because when I first define the technology there was not an ID generated till I call the savechanges() method which physically created the technology object and get it value . so first I was expecting the <code>server.TServerID</code> to get a values of 0 ,, but seems the code will work well. So can anyone adivce on this please?.
  I checked the sql profiler and it showed the following 2 insert operations:-</p>
  
  <pre><code>exec sp_executesql N'insert [dbo].[Technology]([Tag], [IsDeleted], [TypeID], [StartDate], [IT360ID], [IsCompleted], [PartialTag], [IsManaged])
  values (@0, @1, @2, @3, @4, @5, @6, @7)
  select [TechnologyID], [timestamp]
  from [dbo].[Technology]
  where @@ROWCOUNT &gt; 0 and [TechnologyID] = scope_identity()',N'@0 nvarchar(50),@1 bit,@2 int,@3 datetime2(7),@4 bigint,@5 bit,@6 bigint,@7 bit',@0=N'H10020789904',@1=0,@2=2,@3='2014-12-20 00:55:43.1567755',@4=47404,@5=1,@6=10020789904,@7=0
  
  
  exec sp_executesql N'insert [dbo].[TServers]([TServerID], [ServerModelID], [ILOIP], [RackID], [StatusID], [BackUpStatusID], [RoleID], [OperatingSystemID], [VirtualCenterID], [Comment], [IT360SiteID])
  values (@0, @1, null, null, null, null, @2, null, null, null, @3)
  select [timestamp]
  from [dbo].[TServers]
  where @@ROWCOUNT &gt; 0 and [TServerID] = @0',N'@0 int,@1 int,@2 int,@3 bigint',@0=1510,@1=2,@2=3,@3=2102
  </code></pre>