开发者

how do add a single entry for two tables using linq to entities

how do add a single entry for two tables using linq to entities

I have table called

                    product

                    product_id
                    product_Name
                    product_Price
                    product_Description
                    product_image
                    category_id

another table      category
                   category_id
                   category_name

i have a new form with textboxes like

                                  txtprodname
                                  txtproddescrip
                                  txtproductprice
                                  picturebox1
               开发者_运维百科                   txtcategoryname

i am trying to add the new product to producttable by using following code

i am using entity framework..

     public byte[] imageToByteArray(System.Drawing.Image image)
    {
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();
    }


    private void btnSave_Click(object sender, EventArgs e)
    {
        Image image = pictureBox1.Image;
        byte[] bit = null;

        bit = imageToByteArray(image);

        product pd = new product();
        pd.product_Name = txtprodname.Text;
        decimal price = Convert.ToDecimal(txtproductprice.Text);
        pd.product_Price = price;
        pd.product_Description = txtproddescrip.Text;           
        pd.product_Image = bit;
        tsgentity.AddToproducts(pd);
        tsgentity.SaveChanges();
        this.Close();       

   }

I am trying to add new product to the product table .. but i dont have any idea how to add the category name to category table .......by using linq

how do i add category name to category table and update the product table with category name that i have entered...

how do update the category id in product table with this name entered along with the new product added to the product table

can any one help on this ....

i am using winforms .....and c# language

this is my entity diagram....


I assume you want to first check if the entered category already exists and want to reuse it if it does. I also assume tsgentity is your DbContext:

string categoryName = category_name.Text;
Category category = tsgentity.Categories.FirstOrDefault(c => c.category_name == categoryName );
if (category == null)
    category = new Category() { category_name = categoryName };

pd.category = category;
tsgentity.Categories.Add(category);

The above code should be executed before you run tsgentity.SaveChanges().


If you have mapped everything correctly, you should be able to do something like this:

var pd = new Product();
//Set properties of product

var c = new Category { Name = txtCategoryName.Text, Description = txtCategoryDescription.Text };
pd.Category = c;

tsgentity.AddToproducts(pd);
tsgentity.SaveChanges();

Hope this helps. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜