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