Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Linq asp.net c在InsertOnSubmit()上的两个不同表中插入数据#_C#_Asp.net_Mysql_Database Design_Linq To Sql - Fatal编程技术网

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

C# 使用Linq asp.net c在InsertOnSubmit()上的两个不同表中插入数据#,c#,asp.net,mysql,database-design,linq-to-sql,C#,Asp.net,Mysql,Database Design,Linq To Sql,我正在用asp.NETC#和Mysql做一个网上购物项目。我正在使用Devart Linqconnect(LinqtoMysql)。我在mysql客户和客户地址中有两个表: 客户表 CustomerID Int 客户Hone varchar(20) 客户密码Varchar(20) 电子邮件varchar(20) 名字字符(50) 姓氏(50) 用户名varshar(50) 客户地址 客户地址ID INT无符号非空自动增量, 客户ID INT不为空, 地址1字符(250), 地址2字符(250),

我正在用asp.NETC#和Mysql做一个网上购物项目。我正在使用Devart Linqconnect(LinqtoMysql)。我在mysql客户和客户地址中有两个表:

客户表
CustomerID Int
客户Hone varchar(20)
客户密码Varchar(20)
电子邮件varchar(20)
名字字符(50)
姓氏(50)
用户名varshar(50)

客户地址
客户地址ID INT无符号非空自动增量,
客户ID INT不为空,
地址1字符(250),
地址2字符(250),
城市字符(20),
州(20), pincode VARCHAR(20),
主键(客户地址ID),
外键(客户ID)引用客户(客户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工具包的模式弹出面板,并在面板中添加了注册和登录表

我的注册小组:


先谢谢你

似乎您必须先将记录插入
客户表
,然后插入
客户地址
,以使数据库引擎能够找到外键

似乎您必须先将记录插入
客户表
,然后进入
Customer\u Addresses
,使db引擎能够在查看自己代码中的一行时找到外键

data.Customers.InsertOnSubmit(NewCustomer);
这说明在单个表上调用了
InsertOnSubmit
InsertAllOnSubmit
。我自己还没有试过,但你可以自己解决,并制定自己的方法来为你做到这一点

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


希望这有帮助。

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

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(),或者是否需要两个,如图所示。我希望你能提供最新情况

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

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

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

只是想一想。

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

 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(),或者是否需要两个,如图所示。我希望你能提供最新情况

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

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

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

只是想一想。

我现在已经尝试过了,但我遇到了一个可怕的错误:viewstate MAC验证失败。如果此应用程序由Web场或群集托管,请确保配置指定了相同的validationKey和验证算法。“自动生成”无法在群集中使用。很抱歉问了这么长的问题。我现在已经尝试过了,但遇到了一个可怕的错误:viewstate MAC验证失败。如果此应用程序由Web场或群集托管,请确保配置指定了相同的validationKey和验证算法。AutoGenerate无法在集群中使用。很抱歉,问题太长了。.C:数据正在MySql My_aspnet_users表中插入,而不是在customers和CustomerAddresses表中插入。。在newCustomer上。地址=newaddress;错误:它不包含newCustomer.Address的定义。请帮助。我只是猜测了newCustomer.Address。您需要查看对象,并查看实际的导航属性是什么。我无法知道那是什么。如果您的Customer对象没有可设置的Address属性,请告诉我。有一个属性为“NewCustomer.CustomerAddresses”,但CustomerAddresses是一个实体集。我将重新表述我的问题,因为我只想在customer表中存储客户信息(而不是地址信息),但将registration panel的地址信息存储到Addresses表中,但只使用客户的Id引用customerlinq2sql在两个表之间创建关联,由newCustomer.CustomerAddresses表示。如果您调用newCustomer.CustomerAddresses.InsertOnSubmit(newAddress),它知道您指的是CustomerAddresses表,当您调用submitchanges()时,它会将其插入其中,并自动将其与您添加到的客户关联。我将在我的答案中编辑代码。非常感谢您的指导。。非常感谢您的帮助C:数据正在MySql My_aspnet_users表中插入,而不是在customers和CustomerAddresses表中插入。。在newCustomer上。地址=newaddress;错误:它不包含newCustomer.Address的定义。请帮助。我只是猜测了newCustomer.Address。您需要查看对象,并查看实际的导航属性是什么。我无法知道那是什么。如果您的客户对象没有可以设置的Address属性,请告诉我