简体   繁体   English

使用Linq2Sql使用自动递增的主键将数据插入多个表

[英]Using Linq2Sql to insert data into multiple tables using an auto incremented primary key

I have a Customer table with a Primary key (int auto increment) and an Address table with a foreign key to the Customer table. 我有一个Customer表,其中包含一个主键(int auto increment)和一个带有Customer表外键的Address表。 I am trying to insert both rows into the database in one nice transaction. 我试图在一个不错的事务中将两行插入数据库。

using (DatabaseDataContext db = new DatabaseDataContext())
{
    Customer newCustomer = new Customer()
    {
        Email = customer.Email
    };

    Address b = new Address()
    {
        CustomerID = newCustomer.CustomerID,
        Address1 = billingAddress.Address1
    };

    db.Customers.InsertOnSubmit(newCustomer);
    db.Addresses.InsertOnSubmit(b);
    db.SubmitChanges();
}

When I run this I was hoping that the Customer and Address table automatically had the correct keys in the database since the context knows this is an auto incremented key and will do two inserts with the right key in both tables. 当我运行这个时,我希望Customer和Address表自动在数据库中拥有正确的密钥,因为上下文知道这是一个自动递增的密钥,并且将在两个表中使用右键进行两次插入。

The only way I can get this to work would be to do SubmitChanges() on the Customer object first then create the address and do SubmitChanges() on that as well. 我可以让它工作的唯一方法是首先在Customer对象上执行SubmitChanges()然后创建地址并对其执行SubmitChanges()。 This would create two roundtrips to the database and I would like to see if I can do this in one transaction. 这将创建两个到数据库的往返,我想看看我是否可以在一个事务中执行此操作。 Is it possible? 可能吗?

Thanks 谢谢

Klaus basically already specified what you need to do - try this code: 克劳斯基本上已经指定了你需要做的事 - 试试这段代码:

using (DatabaseDataContext db = new DatabaseDataContext())
{
    Customer newCustomer = new Customer()
    {
        Email = customer.Email
    };

    Address b = new Address()
    {
        Address1 = billingAddress.Address1
    };

    newCustomer.Address = b;

    db.Customers.InsertOnSubmit(newCustomer);
    db.SubmitChanges();
}

If you associate the address b with the customer you've just created, and then insert that customer into the db.Customers collection, calling db.SubmitChanges() should automatically save the address, save the customer and fix up any of the IDENTITY columns to make this work. 如果将地址b与您刚创建的客户关联,然后将该客户插入db.Customers集合,则调用db.SubmitChanges()应自动保存地址,保存客户并修复任何IDENTITY列使这项工作。 It does work for me in a test case, for sure. 在测试用例中,它确实适用于我。

You cannot use the address' or customer's ID just yet - those haven't been set yet. 您尚不能使用地址'或客户的ID - 尚未设置的地址。 But you can definitely associate the full objects with one another and thus get the "connection" between the two in place. 但是你绝对可以将完整的对象相互关联,从而获得两者之间的“连接”。

For this to work, you need to make sure that in the DBML designer, in the Properties window for both ID columns, the Auto Generated Value is True and the Auto-Sync is ste to OnInsert (both of which aren't the defaults). 为此,您需要确保在DBML设计器中,在两个ID列的“ Properties窗口中,“ Auto Generated ValueTrue ,“ Auto-Sync为“ OnInsert (两者都不是默认值) 。

If you have a foreign key relationship in the database the Customer object should have a collection called Addresses which you can Add your address instance called b to. 如果数据库中有外键关系, Customer对象应该有一个名为Addresses的集合,您可以Add名为b的地址实例Add到。 If you do this, you don't have to explicitly add the address to db.Addresses it will be added by the framework automatically, and the right customer id will be inserted. 如果这样做, db.Addresses必将地址显式添加到db.Addresses ,它将由框架自动添加,并将插入正确的客户ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM