INSERT语句与FOREIGN KEY约束冲突异常,但存储的数据

发布于 2024-10-22 11:34:48 字数 5640 浏览 2 评论 0原文

INSERT 语句与 FOREIGN KEY 约束“FK_JobList_aspnet_Membership”冲突。冲突发生在数据库“C:\JOBPOST\APP_DATA\ASPNETDB.MDF”,表“dbo.aspnet_Membership”,列“UserId”。该语句已终止。

它总是抛出此异常,但数据已已成功存储在数据库中。我用不同的方法多次测试这种情况。现在我在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();

异常详细信息:System.Data.SqlClient.SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_JobList_aspnet_Membership”冲突。冲突发生在数据库“C:\JOBPOST\APP_DATA\ASPNETDB.MDF”、表“dbo.aspnet_Membership”、“UserId”列中。

该声明已终止。

来源错误:

执行当前 Web 请求期间生成未处理的异常。有关异常来源和位置的信息可以使用下面的异常堆栈跟踪来识别。

堆栈跟踪:

[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

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.

It always throws this exception, but the data has been successfully stored in the database.I test the situation many times with diffirent method.Right now I only have 2 UserId in aspnet_membership, Use that uniqueidentifier UsrId column as the FK_JobList_aspnet_Membership. The primary Key table is aspnet_membership, the foreign key table is my JobList, which itself has index JobId primary key

I can't figure out where i am wrong. 2 UsrId only from aspnet.mdf, just add 1 joblist table,why it always throw exception, but successfully store the data.

The code:

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.

Source Error:

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.

Stack Trace:

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2024-10-29 11:34:48

我要做的第一件事就是在提交更改的代码上放置一个调试器,然后查看它是否被调用两次。

听起来可能会成功执行一次,然后第二次就会失败。

The very first thing I would do is put a debugger on the code that submits changes and then see if it's called twice.

Sounds like it might be executed once successfully, then a second time with a failure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文