在InsertOnSubmit()上的两个不同表中插入数据使用Linq asp.net c#

时间:2013-03-16 17:48:25

标签: c# asp.net mysql database-design linq-to-sql

我正在开发一个使用asp.net c#和Mysql进行在线购物的项目。我正在使用Devart Linqconnect(LinqtoMysql)。我在mysql客户和客户addreesses中有两个表:

客户表
CustomerID Int
Customerhone varchar(20);
客户密码Varchar(20);
email varchar(20)
名字c​​har(50)
姓氏(50)
username varshar(50)

Customer_Addresses
  Customer_address_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
  Customer_ID INT NOT NULL,
  address1 CHAR(250),
  address2 CHAR(250),
  城市CHAR(20),
  CHAR(20),   pincode VARCHAR(20),
  PRIMARY KEY(Customer_address_ID),
  FOREIGN KEY(Customer_ID)REFERENCES customers(Customer_ID)

当我使用LINQ to mysql注册客户时编写此代码:

 using (ShoppingDataContext data = new ShoppingDataContext())
        {
            Customer NewCustomer = new Customer();
            CustomerAddress newaddress = new CustomerAddress();
            newaddress.CustomerID = NewCustomer.CustomerID;
            NewCustomer.CustomerFirstname = TextBoxFirstName.Text;
            NewCustomer.CustomerLastname = TextBoxLastname.Text;
            NewCustomer.CustomerEmail = TextBoxEmail.Text;
            NewCustomer.Username = TextBoxusername.Text;
            NewCustomer.CustomerPassword = TextBoxPassword.Text;
            newaddress.Address1 = TextBoxAddress1.Text;
            newaddress.Address2 = TextBoxAddress2.Text;
            newaddress.City = TextBoxCity.Text;
            newaddress.State = TextBoxState.Text;
            newaddress.Pincode = TextBoxPincode.Text;
            System.Web.Security.Membership.CreateUser(TextBoxusername.Text, TextBoxPassword.Text);
            data.Customers.InsertOnSubmit(NewCustomer);
            PanelRegister.Visible = false;
            ConfimPanel.Visible = true;

        }

此代码是否可以将数据插入到两个表中。请帮忙。 customer_address表是否能够根据customerId作为外键检测到输入的客户地址是customer表..

我正在使用ajax工具包的模态弹出面板,并在面板中添加了注册和登录表..

我的注册小组: enter image description here

提前致谢...

3 个答案:

答案 0 :(得分:1)

您似乎必须先将记录插入Customer Table,然后再插入Customer_Addresses以使数据库引擎能够找到外键

答案 1 :(得分:1)

如果您查看自己代码中的一行,

data.Customers.InsertOnSubmit(NewCustomer);

这告诉您在单个表上调用InsertOnSubmitInsertAllOnSubmit的故事。我自己没有尝试过,但你可以解决这个问题并制定自己的方法来为你做这件事。

尽管正如Pablo Lemurr所提到的那样,在引用外键之后,您需要先添加客户记录,然后再添加地址记录。

希望这有帮助。

答案 2 :(得分:1)

我强烈建议您按照以下方式组织代码:

 using (ShoppingDataContext data = new ShoppingDataContext())
 {
     Customer newCustomer = new Customer()
     {
         CustomerFirstname = TextBoxFirstName.Text,
         CustomerLastname = TextBoxLastname.Text,
         CustomerEmail = TextBoxEmail.Text,
         Username = TextBoxusername.Text,
         CustomerPassword = TextBoxPassword.Text
     };

     //now I'd like to be proven wrong here, but I believe you need to insert
     //and submit at this point
     data.Customers.InsertOnSubmit(newCustomer);
     data.SubmitChanges();

     CustomerAddress newaddress = new CustomerAddress()
     {
          CustomerID = NewCustomer.CustomerID,
          Address1 = TextBoxAddress1.Text,
          Address2 = TextBoxAddress2.Text,
          City = TextBoxCity.Text,
          State = TextBoxState.Text,
          Pincode = TextBoxPincode.Text,
     };
     //add new address to your customer and save
     newCustomer.CustomerAddresses.Add(newAddress);
     //it has been a while since I used linq2sql so you may need one of these:
     //newCustomer.CustomerAddresses.InsertOnSubmit(newAddress);
     //newCustomer.CustomerAddresses.Attach(newAddress);
     //basically use intellisense to help you figure out the right one, you might
     //have some trial and error here
     data.SubmitChanges();

     System.Web.Security.Membership.CreateUser(TextBoxusername.Text, TextBoxPassword.Text);

     PanelRegister.Visible = false;
     ConfimPanel.Visible = true;

  }

另外,请注意insert和db.SubmitChanges()的移动位置。现在我不确定你是否可以一次使用一个SubmitChanges()或者如果你需要两个如此显示。我对你提供最新消息感兴趣。

您可以在此处查看简单的插入示例:

http://msdn.microsoft.com/en-us/library/bb386941.aspx

编辑:您可能希望将这一切包装在一个事务管理器中,以便它作为一个单元成功或失败

using(TransactionScope scope = new TransactionScope())
{
  using(ShoppingDataContext data = new ShoppingDataContext())
  {
    //the rest of your code
  }
}

只是一个想法。