如何使用实体框架一次将数据插入两个表中
我需要插入Windows窗体中的数据,必须插入到两个表中。我正在Windows窗体中的所有控件使用数据源。能不能请我向我推荐如何将Windows窗体中的数据同时插入两个表中?
i need to insert data which is in windows forms has to insert to two tables. i am using data source for all controls in windows forms. can you please sugest me how to insert data from windows forms into two tables at same time.
我的表就是这样
**product table**
pid salePrice
**Product_tax table**
pid taxid
当我单击提交按钮时,产品ID将自动生成且salePrice要同时从表单存储什么,我选择的税收也必须与产品ID一起存储在product_tax表中。请从这一点上帮助我。
When i click on submit button product id will auto generate and salePrice has to store from form at same time what ever i select tax that also has to store in product_tax table along with product id. please help me out from this point.
提前谢谢。
因此,为了便于您执行此操作,这是一个简单的示例,以便您可以理解(有更好,更复杂的方法)。
So in order for you to do so, this is a simple example so that you can understand (there are better and more sophisticated ways to do this).
private void AddProducts(double salePrice, int taxValue)
{
using (var ctx = new Entity())
{
Product prodObject = new Product
{
PId = //NEW ID,
SalePrice = salePrice
};
Product_tax pTax = new Product_tax
{
pid = prodObject.PId,
taxid = taxValue
};
ctx.product.AddObject(prodObject);
ctx.Product_tax .AddObject(pTax);
ctx.SaveChanges();
}
}
这应该通过调整解决方案来解决
This should do the trick with a tweak to your solution.