Umer Khalid
Umer Khalid

Reputation: 99

Inserting data in child table using Linq to sql (3 tier Architecture)

I am stuck at a simple code in asp.net I want to insert simple string values to 1 Parent Table (Accounts) and 1 Child Table(Applicant_bio). Now here's the thing, I am able to put data in Parent table but when I try to access the Child table, it gives me the following error:

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

I have explicitly set the values of both Primary Keys to match so there is no conflict, as the tables have a 1 to 1 relation. Here's My code:

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";
        }

Here's the Database Details enter image description here

Upvotes: 0

Views: 883

Answers (1)

Eric
Eric

Reputation: 91

The error happens because linq understand that with applicant_bio you are trying to save also a new account objet that already exist, try saving both it works this way:

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";
        }

Upvotes: 1

Related Questions