[英]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.