我正在尝试升级 EF 数据库架构。架构升级后,表、索引和键看起来都正确,但是当我运行应用程序时,我发现所有插入都失败(更新工作正常),包括我没有更改的表。例如,我有一个由这个脚本生成的表:
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);
但是当我尝试添加新的博客文章时,我看到以下错误:
Cannot insert the value NULL into column 'Id', table 'ptytest.dbo.BlogPosts';
column does not allow nulls. INSERT fails.
Intellitrace 显示以下插入:
ADO.NET:Execute Reader "insert [dbo].[BlogPosts]([Date], [Title], [Post], [Category])
values (@0, @1, @2, @3)
select [Id]
from [dbo].[BlogPosts]
where @@ROWCOUNT > 0 and [Id] = scope_identity()"
具有新架构的干净数据库适用于类似的 SQL。不知何故,这在升级的架构中没有正确处理身份密钥。知道发生了什么吗?
这是原始 db.SaveChanges() 的堆栈跟踪
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& 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& task, Boolean asyncWrite) +1379
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& 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