简体   繁体   English

使用Linq-to-SQL的INSERT上的外键异常,但不知道为什么吗?

[英]Foreign key exception on INSERT with Linq-to-SQL but can't figure out why?

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_JobList_aspnet_Membership". INSERT语句与FOREIGN KEY约束“ 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. 数据库“ C:\\ JOBPOST \\ APP_DATA \\ ASPNETDB.MDF”的表“ dbo.aspnet_Membership”的“ UserId”列中发生冲突。该语句已终止。

My FK_JobList_aspnet_Membership setup is: 我的FK_JobList_aspnet_Membership设置是:

  • Primary table ( aspnet_membership ) 主表( aspnet_membership
  • foreign key table ( JobList ) 外键表( JobList
  • use column UserId(uniqueidentifier) for both tables, currently only set 2 UserId 对两个表都使用列UserId(uniqueidentifier) ,当前仅设置2个UserId

JobList has its auto indexable primary key int JobId . JobList具有自动索引的主键int JobId No Action on Insert and update attribute, Delete set to Cascade 对插入和更新属性无操作,将删除设置为级联

In the dbml, I also set UpdateCheck=UpdateCheck.Never for JobList Table 在dbml中,我还为JobList表设置了UpdateCheck=UpdateCheck.Never

I don't understand why the exception happens, since I use membership service after user login. 我不明白为什么会发生异常,因为我在用户登录后使用了会员服务。 It should have no conflict. 它应该没有冲突。 BTW, I am sure there is no other duplicate function for Inserting Item. 顺便说一句,我确定插入项目没有其他重复功能。 Though throw exception, the new row data has been successfully inserted with correct userId. 尽管抛出异常,但是新行数据已使用正确的userId成功插入。 It keeps throwing this kind of exception for each time Inserting Item. 每次插入Item时,都会不断抛出这种异常。

protected void LinqDataSourceDetail_Inserting(object sender, LinqDataSourceInsertEventArgs e) 
{        
    if (Page.IsValid == true)     
    {               
        JobPostDataContext db = new JobPostDataContext();

        JobList newJob = new JobList();
        newJob.JobTitle = ((TextBox)DetailsView1.FindControl("TB_JobTitle")).Text;
        newJob.Summary = ((TextBox)DetailsView1.FindControl("TB_Summary")).Text;
        newJob.Detail = ((TextBox)DetailsView1.FindControl("TB_Detail")).Text;
        newJob.CompanyName = ((TextBox)DetailsView1.FindControl("TB_CompanyName")).Text;
        newJob.CompanyEmail = ((TextBox)DetailsView1.FindControl("TB_CompanyEmail")).Text;
        String date = ((TextBox)DetailsView1.FindControl("TB_PostDate")).Text;
        newJob.PostDate = (DateTime)Convert.ToDateTime(date);
        newJob.IsTop = false;
        newJob.UserId = (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey;

        db.JobLists.InsertOnSubmit(newJob); 
        db.SubmitChanges();          
     } 
}

It means that the guid returned by (Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey doesn't appear in the aspnet_membership table. 这意味着(Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey返回的(Guid)Membership.GetUser(User.Identity.Name).ProviderUserKey不会出现在aspnet_membership表中。

This is probably because Membership.GetUser(User.Identity.Name) is not finding the current user. 这可能是因为Membership.GetUser(User.Identity.Name)未找到当前用户。 In turn this is probably because User.Identity.Name is not in the aspnet_membership table either. 反过来这可能是因为User.Identity.Name也不在aspnet_membership表中。

Suggested resolution: 建议的分辨率:

  • output User.Identity.Name. 输出User.Identity.Name。 One way to do this is to throw new Exception(User.Identity.Name) 一种方法是throw new Exception(User.Identity.Name)
  • Check that the name appears in the membership table. 检查名称是否出现在成员资格表中。

When you inevitably discover that it does not, you can continue your debugging from there. 当您不可避免地发现它不是时,可以从那里继续进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM