0

假设我在客户和产品之间有 1:1 的关系。所以我要为两者添加一个表。在我的 LINQ to SQL 文件中,我用 .dbml 中的键和关联正确设置了所有内容。

现在我插入两个应该关联的表:

    CustomerDataContext dc = new CustomerDataContext();
    Table<Customer> customers = dc.GetTable<Customer>();
    Table<Product> products= dc.GetTable<Product>();

    Customer newCustomer = new Customer ();
    Product newProduct   = new Product();


    customers.InsertOnSubmit(newCustomer);
    products.InsertOnSubmit(newProduct);
    customers.Context.SubmitChanges();

那么此时我该如何建立关联呢?因此,当我查询此客户表时,我可以调用关联的产品,例如 Customer.Product?

这甚至可能吗?

打招呼

4

1 回答 1

0
CustomerDataContext dc = new CustomerDataContext();
Table<Customer> customers = dc.GetTable<Customer>();
Table<Product> products= dc.GetTable<Product>();
Table<ProductConsumer> ProductConsumers= dc.GetTable<ProductConsumer>();

Customer newCustomer = new Customer ();
Product newProduct   = new Product();


customers.InsertOnSubmit(newCustomer);
products.InsertOnSubmit(newProduct);
customers.Context.SubmitChanges();
products.Context.SubmitChanges();

//Now insert associations
ProductConsumer= new ProductConsumer();
ProductConsumer.Consumer= newCustomer ;
ProductConsumer.Product= newProduct  ;


ProductConsumers.InsertOnSubmit(ProductConsumer)
ProductConsumers.Context.SubmitChanges();
于 2012-08-24T23:07:08.803 回答