title: Entity Framework 5 fails all inserts with Cannot insert the value NULL into column 'Id'
body: <p>I am trying to upgrade an EF db schema.  After the schema upgrade, the tables, indexes and keys all look correct, but when I run the application I find that all inserts fail (updates work fine) including on tables I didn't change.  For example, I have a table generated by this script:</p>
  
  <pre><code>CREATE TABLE [dbo].[BlogPosts] (
  [Id] int IDENTITY(1,1) NOT NULL,
  [Date] datetime  NOT NULL,
  [Title] nvarchar(max)  NOT NULL,
  [Post] nvarchar(max)  NOT NULL,
  [Category] nvarchar(max)  NOT NULL);
  
  ALTER TABLE [dbo].[BlogPosts]
  ADD CONSTRAINT [PK_BlogPosts]
  PRIMARY KEY CLUSTERED ([Id] ASC);
  </code></pre>
  
  <p>But when I try to add a new blog post, I see the following error:</p>
  
  <pre><code>Cannot insert the value NULL into column 'Id', table 'ptytest.dbo.BlogPosts'; 
  column does not allow nulls. INSERT fails.
  </code></pre>
  
  <p>Intellitrace shows the following insert:</p>
  
  <pre><code>ADO.NET:Execute Reader "insert [dbo].[BlogPosts]([Date], [Title], [Post], [Category])
  values (@0, @1, @2, @3)
  select [Id]
  from [dbo].[BlogPosts]
  where @@ROWCOUNT &gt; 0 and [Id] = scope_identity()"
  </code></pre>
  
  <p>A clean DB with the new schema works fine with similar SQL.  Somehow this is not treating the identity key properly in the upgraded schema.  Have any idea what is going on?</p>
  
  <p>Here is the stack trace from the original db.SaveChanges()</p>
  
  <pre><code>System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1753986
  System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5296058
  System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558
  System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady) +1682
  System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +59
  System.Data.SqlClient.SqlDataReader.get_MetaData() +90
  System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +365
  System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean asyncWrite) +1379
  System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean asyncWrite) +175
  System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
  System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134
  System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
  System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10
  System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) +217
  System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +262
  
  [UpdateException: An error occurred while updating the entries. See the inner exception for details.]
  System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) +444
  System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) +146
  System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) +571
  System.Data.Entity.Internal.InternalContext.SaveChanges() +323
  
  [DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
  System.Data.Entity.Internal.InternalContext.SaveChanges() +369
  System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +53
  System.Data.Entity.DbContext.SaveChanges() +52
  </code></pre>