【发布时间】:2016-05-19 18:00:06
【问题描述】:
我被 asp.net 中的一个简单代码困住了 我想将简单的字符串值插入到 1 个父表(帐户)和 1 个子表(Applicant_bio)。 现在是这样,我可以将数据放入父表中,但是当我尝试访问子表时,它给了我以下错误:
The INSERT statement conflicted with the FOREIGN KEY constraint linq to sql
我已明确将两个主键的值设置为匹配,因此不存在冲突,因为表具有 1 对 1 的关系。 这是我的代码:
public string Retreive_Applicants(Applicant_list user_details)
{
newDatabaseDataContext connection = new newDatabaseDataContext();
//Create a new instance of the applicant object
account account = new account();
account.account_id = 1;
account.account_type = "Applicant";
account.account_description = "";
account.account_title = user_details.account_title;
account.account_password = user_details.account_password;
connection.accounts.InsertOnSubmit(account);
connection.SubmitChanges();
account.applicant_bio= new applicant_bio();
account.applicant_bio.account_id = account.account_id; //Here's Where I have explicitly set the account id of applicant_bio to account_id of accounts table just created
account.applicant_bio.applicant_name = user_details.applicant_name;
account.applicant_bio.applicant_age = user_details.applicant_age;
account.applicant_bio.applicant_cnic = user_details.applicant_cnic;
connection.applicant_bios.InsertOnSubmit(account.applicant_bio);
connection.SubmitChanges(); //Here's where the error occurs
return "success";
}
这是数据库详细信息 enter image description here
【问题讨论】:
-
您似乎在子表中插入了一个不在父表中的键,请尝试打印值并查看
-
@TheGameiswar 打印了两个对象值并且它们返回相同的值,即 account.account_id 和 account.applicant_bio.account_id 返回相同的值
-
如果你同时给出两个表的 db 定义会有所帮助。
-
@RoyFalk 我已在原帖中添加了一张图片,请查看
-
我以网站审阅者的身份写了这篇评论,目的是提高问题的质量,从而吸引有知识的人来帮助你。唉,SQL 从来都不是我熟悉的东西。不过看图后,这还不如文字定义。
标签: c# mysql asp.net sql-server linq