【问题标题】:Inserting data in child table using Linq to sql (3 tier Architecture)使用 Linq to sql 在子表中插入数据(3 层架构)
【发布时间】: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


【解决方案1】:

发生错误是因为 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";
        }

【讨论】:

  • 我已经这样做了,还是不行,我还想补充一点,我在accounts表中的account_idapplicant_bio表中的applicant_id之间创建了一个关系(1到 1)。我也尝试将account_id 设置为两个表的主键,但它仍然给我一个外键错误。但是,如果我在accounts 表(父)中的account_idapplicant_bio 表(子)中的account_id 之间创建一对多关系,我可以成功输入数据
猜你喜欢
  • 2011-10-02
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-24
相关资源
最近更新 更多