The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobList_aspnet_Membership". The conflict occurred in database "C:\JOBPOST\APP_DATA\ASPNETDB.MDF", table "dbo.aspnet_Membership", column 'UserId'.The statement has been terminated.
INSERT語句衝突與外鍵約束的例外,但數據存儲
它總是拋出此異常,但數據已經成功地存儲在database.I多次與向度method.Right測試情況現在我在aspnet_membership中只有2個UserId,使用該uniqueidentifier UsrId列作爲FK_JobList_aspnet_Membership。主鍵表是aspnet_membership,外鍵表是我的JobList,它本身有索引JobId主鍵
我找不出我錯在哪裏。 2 UsrId只能從aspnet.mdf中,只添加1個joblist表,爲什麼它總是拋出異常,但成功存儲數據。
代碼:
JobPostDataContext db = new JobPostDataContext();
JobList newJob = new JobList();
MembershipUser curUser = Membership.GetUser(User.Identity.Name);
Guid user = new Guid(curUser.ProviderUserKey.ToString());
newJob.UserId =user;
newJob.JobTitle = ((TextBox)DetailsView1.FindControl("TB_JobTitle")).Text;
db.JobLists.InsertOnSubmit(newJob);
db.SubmitChanges();
Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobList_aspnet_Membership". The conflict occurred in database "C:\JOBPOST\APP_DATA\ASPNETDB.MDF", table "dbo.aspnet_Membership", column 'UserId'.
The statement has been terminated.
源錯誤:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
堆棧跟蹤:
[SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobList_aspnet_Membership". The conflict occurred in database "C:\DOCUMENTS AND SETTINGS\QI\DESKTOP\CS526\JOBPOST\APP_DATA\ASPNETDB.MDF", table "dbo.aspnet_Membership", column 'UserId'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +86
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.ExecuteReader() +12
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +1266
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
System.Data.Linq.StandardChangeDirector.DynamicInsert(TrackedObject item) +151
System.Data.Linq.StandardChangeDirector.Insert(TrackedObject item) +235
System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +337
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +378
System.Data.Linq.DataContext.SubmitChanges() +23
System.Web.UI.WebControls.LinqToSqlWrapper.SubmitChanges(DataContext dataContext) +9
System.Web.UI.WebControls.LinqDataSourceView.InsertDataObject(Object dataContext, Object table, Object newDataObject) +89
System.Web.UI.WebControls.LinqDataSourceView.InsertObject(Object newEntity) +204
System.Web.UI.WebControls.QueryableDataSourceView.ExecuteInsert(IDictionary values) +105
System.Web.UI.WebControls.ContextDataSourceView.ExecuteInsert(IDictionary values) +94
System.Web.UI.WebControls.LinqDataSourceView.ExecuteInsert(IDictionary values) +29
System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +89
System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +379
System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +574
System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +95
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +112
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +125
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +169
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +9
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563