title: C# ADO.NET Determine Value Of New Auto Increment Primary Key?
body: <p>I am just designing a web service at the moment so I have no actual code but this potentially affects both the database and application design.</p>
  
  <p>I plan on having a database table (in SQL Server) which will have a primary key of type bigint which is auto increment. Now I'm assuming in ADO.NET I won't be setting this field when adding new records to the table. What I need to know is how will I know what the ID was that was just added. Of course I could then do a select MAX on that field but does ADO.NET actually return the value of the new ID saving me the overhead?</p>
  
  <p>For example, if I had some code like this:</p>
  
  <pre><code>using (MyEntities context = new MyEntities())
  {
      context.Table1.AddObject(new Table1()
      {
          StringColumn1 = "some value",
          StringColumn2 = "some value"
      });
  
      context.SaveChanges();
  }
  </code></pre>
  
  <p>If there was a auto increment PK in Table1 called ID, how do I know the value that was just added?</p>