非常简单。鉴于您在表之间定义了关系,您可以执行以下操作:
using ( Entity EF = new Entity()){
EF.addToProductTax(new ProductTax(){
Product = new Product(){
pid = //your generated Product id,
salePrice = price
},
Tax = (FROM t in EF.tax where t.taxid == taxid select t);
});
}
为了更容易理解:
Product item = new Product();
item.salePrice = price;
// pid gets generated automatically !
Tax correspondingTax = (from t in EF.tax where t.taxid == taxid select t);
Product_Tax add = new Product_Tax;
add.Product = item;
add.Tax = correspondingTax;
EF.addToProductTax(add);
请记住,这仅在您在两个表之间定义了关系时才有效。在其他所有情况下,您都必须这样做:
EF.addToTax(new Tax(){
taxid = taxid,
// and all the other fields
});
EF.addToProducts(new Product(){
pid = pid,
salePrice = saleprice
});
EF.addToProductTax(new ProductTax(){
pid = pid,
taxid = taxid
});