[英]Insert data in two Different tables on InsertOnSubmit() Using Linq asp.net c#
我正在使用asp.net c#和Mysql进行在线购物项目。 我正在使用Devart Linqconnect(LinqtoMysql)。 我在mysql客户和customer地址中有两个表:
客户表
客户ID整数
Customerhone varchar(20);
客户密码Varchar(20);
电子邮件varchar(20)
姓char(50)
姓氏(50)
用户名varshar(50)
客户地址
Customer_address_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
Customer_ID INT NOT NULL,
地址1 CHAR(250),
地址2 CHAR(250),
城市CHAR(20),
状态CHAR(20),密码VARCHAR(20),
主键(Customer_address_ID),
外键(Customer_ID)参考客户(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作为外键来检测输入的客户地址是该客户表的地址。
我正在使用ajax工具箱的模式弹出面板的另一件事,并在面板中添加了注册和登录表。
我的注册小组:
提前致谢...
看来您必须先将一条记录插入到Customer Table
,然后再插入到Customer_Addresses
以使db引擎能够找到外键
如果您从自己的代码中查看一行,
data.Customers.InsertOnSubmit(NewCustomer);
这告诉您在单个表上调用InsertOnSubmit
和InsertAllOnSubmit
的故事。 我自己没有尝试过,但是您可以解决此问题,并采用自己的方法为您完成此操作。
尽管正如Pablo Lemurr所述,您将需要先添加客户记录,然后再添加地址记录,因为它引用了外键。
希望这可以帮助。
我强烈建议您按以下方式组织代码:
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/zh-CN/library/bb386941.aspx
编辑:您可能希望将其全部包装在transactionscope中,以便它作为一个单元成功或失败
using(TransactionScope scope = new TransactionScope())
{
using(ShoppingDataContext data = new ShoppingDataContext())
{
//the rest of your code
}
}
只是一个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.