0

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

客户表
CustomerID Int
Customerhone varchar(20);
客户密码 Varchar(20);
电子邮件 varchar(20)
名字 char(50)姓氏(
50)
用户名​​ varshar(50)

Customer_Addresses
Customer_address_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
Customer_ID INT NOT NULL,
address1 CHAR(250),
address2 CHAR(250),
city CHAR(20),
state 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 工具包的模态弹出面板并在面板中添加注册和登录表的另一件事..

我的注册面板: 在此处输入图像描述

提前致谢...

4

3 回答 3

1

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

于 2013-03-16T17:54:59.110 回答
1

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

data.Customers.InsertOnSubmit(NewCustomer);

这告诉您在单个表上调用InsertOnSubmitand的故事。InsertAllOnSubmit我自己没有尝试过,但是您可以解决并制定自己的方法来为您执行此操作。

尽管正如 Pablo Lemurr 所提到的,您需要先添加客户记录,然后再添加地址记录,因为它引用了外键。

希望这可以帮助。

于 2013-03-16T18:02:45.577 回答
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
  }
}

只是一个想法。

于 2013-03-16T18:17:03.043 回答