使用Linq to sql(3层体系结构)在子表中插入数据

时间:2016-02-09 16:37:03

标签: c# mysql asp.net sql-server linq

我被困在asp.net中的一个简单代码中 我想将简单的字符串值插入1个父表(帐户)和1个子表(Applicant_bio)。 现在,我可以将数据放在父表中,但是当我尝试访问Child表时,它会给我以下错误:

[Code]

procedure InitializeUninstallProgressForm();
begin
  UninstallProgressForm.Caption := 'Uninstalling';
end;

我已明确设置两个主键的值以匹配,因此没有冲突,因为表具有1对1的关系。 这是我的代码:

The INSERT statement conflicted with the FOREIGN KEY constraint linq to sql

这是数据库详细信息 enter image description here

1 个答案:

答案 0 :(得分:0)

错误发生是因为linq明白,在您尝试保存已存在的新帐户对象时,您尝试保存这两种情况:

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;

           account.applicant_bio= new applicant_bio();
           //You dont need this line any more
           //account.applicant_bio.account_id = account.account_id; 
           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.accounts.InsertOnSubmit(account);
           connection.SubmitChanges();
            return "success";
        }