how to insert data into two tables at a time using entity framework

c#, entity-framework, sql, windows

Solution

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.

Problem

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. my table is like this ``` **product table** pid salePrice **Product_tax table** pid taxid ``` 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. Thanks in advance.

Original source