2013-06-04 31 views
2

我想升級EF數據庫模式。模式升級後,表,索引和鍵都看起來是正確的,但是當我運行應用程序時,我發現所有插入失敗(更新正常),包括我沒有更改的表。例如,我有這個腳本生成的表:實體框架5失敗所有插入無法將值NULL插入到'Id'列

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 
+0

code first or edmx? –

+0

請顯示拋出的代碼無法插入值NULL錯誤。 – TheGeekYouNeed

+0

SQL不會在該表上給出該錯誤。 「Id」一定不能是「IDENTITY」列。 –

回答

0

我只是面臨着同樣的問題。我是從azure導入數據庫的,它不會將關鍵字段作爲關鍵字(Identity)導入。這就是我遇到問題的原因。我所做的工作就是使用「導出數據層應用程序」將數據庫導出到bacpac文件,然後使用「導入數據層應用程序」創建您的開發數據庫。希望能幫助到你。