2017-09-16 53 views
-3

我只想知道這個錯誤是什麼意思? INSERT語句與FOREIGN KEY約束「FK__thumbnail__instr__160F4887」衝突。數據庫「MusicStoreDB」,表「dbo.instrumentItem」,列'instrumentId'發生衝突。 該聲明已被終止。asp.net中的服務器錯誤?

這裏是堆棧跟蹤:

Server Error in '/' Application. 

The INSERT statement conflicted with the FOREIGN KEY constraint 
"FK__thumbnail__instr__160F4887". The conflict occurred in database 
"MusicStoreDB", table "dbo.instrumentItem", column 'instrumentId'. 
The statement has been terminated. 

Description: An unhandled exception occurred during the execution of the 
current web request. Please review the stack trace for more information 
about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: The INSERT statement 
conflicted with the FOREIGN KEY constraint "FK__thumbnail__instr__160F4887". 
The conflict occurred in database "MusicStoreDB", table 
"dbo.instrumentItem", column 'instrumentId'. 
The statement has been terminated. 

Source Error: 


Line 74: 
Line 75:    obj.thumbnailImages.Add(subImg); 
Line 76:    obj.SaveChanges(); 
Line 77:   } 
Line 78:  } 

Source File: c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs Line: 76 

Stack Trace: 


[SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN 
KEY constraint "FK__thumbnail__instr__160F4887". The conflict occurred in 
database "MusicStoreDB", table "dbo.instrumentItem", column 'instrumentId'. 
The statement has been terminated.] 
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean 
breakConnection, Action`1 wrapCloseInAction) +2434922 
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, 
Boolean breakConnection, Action`1 wrapCloseInAction) +5736592 

System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning 
(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean 
asyncClose) +285 
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, 
SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet 
bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731 
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +58 
System.Data.SqlClient.SqlDataReader.get_MetaData() +89 
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, 
RunBehavior runBehavior, String resetOptionsString) +379 
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior 
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, 
Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader 
ds, Boolean describeParameterEncryptionRequest) +2026 
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior 
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, 
TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean 
asyncWrite) +375 
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior 
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
+53 
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, 
String method) +240 
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 
behavior) +41 
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12 
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher. 
<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c) +9 

System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1 
.Dispatch(TTarget target, Func`3 operation, TInterceptionContext 
interceptionContext, Action`3 executing, Action`3 executed) +72 

System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher 
.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) 
+355 
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader 
(CommandBehavior behavior) +167 
System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +12 

System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand. 
Execute(Dictionary`2 identifierValues, List`1 generatedValues) +234 
System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() 
+139 

[UpdateException: An error occurred while updating the entries. 
See the inner exception for details.] 
System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() 
+319 
System.Data.Entity.Core.EntityClient.Internal.EntityAdapter. 
<Update>b__2(UpdateTranslator ut) +9 
System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(T 
noChangesResult, Func`2 updateFunction) +120 
System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() +77 
System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35() 
+11 
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 
func, IDbExecutionStrategy executionStrategy, Boolean 
startLocalTransaction, Boolean releaseConnectionOnSuccess) +288 
System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions 
options, IDbExecutionStrategy executionStrategy, Boolean 
startLocalTransaction) +163 
System.Data.Entity.Core.Objects.<>c__DisplayClass2a. 
<SaveChangesInternal>b__27() +22 
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 
operation) +164 

System.Data.Entity.Core.Objects.ObjectContext. 
SaveChangesInternal(SaveOptions options, Boolean 
executeInExistingTransaction) +222 
System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions 
options) +7 
System.Data.Entity.Internal.InternalContext.SaveChanges() +114 

[DbUpdateException: An error occurred while updating the entries. See the 
inner exception for details.] 
System.Data.Entity.Internal.InternalContext.SaveChanges() +199 
System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +27 
System.Data.Entity.DbContext.SaveChanges() +20 
ConnectionClassGuitarItems.AddThumnailImage(thumbnailImage subImg) in 
c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\App_Code\ConnectionClassGuitarItems.cs:76 
Pages_CreateBrands.itemSaveButton_Click(Object sender, EventArgs e) in 
c:\Users\User1\Documents\Visual Studio 
2015\WebSites\MusicStore\Pages\CreateBrands.aspx.cs:231 
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9696694 
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
+204 

System.Web.UI.WebControls.Button.System.Web.UI. 
IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, 
String eventArgument) +15 
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, 
Boolean includeStagesAfterAsyncPoint) +1639 
+1

異常消息是不言自明的。確保您存儲在instrumentItem表中的instrumentId是有效的(它應該存在於Instrument表中) – Shyju

+1

您是否熟悉外鍵約束是什麼?這是在Google上查找的好用語。基本上錯誤是告訴你,你試圖插入一個值,只允許某些值的列(基於相關表中的值),並且你插入的內容不是允許的值之一(doesn在相關表格中不存在)。 – David

回答

1

這意味着你正在試圖instrumentItem表和你分配給instrumentId列中的值插入一行,是無效的。該表與數據庫中的另一個表之間存在外鍵關係,instrumentId的值應該存在於另一個表中,但不是。