【问题标题】:how do add a single entry for two tables using linq to entities如何使用 linq 为两个表添加单个条目到实体
【发布时间】:2011-08-19 09:42:50
【问题描述】:

我有个表叫

                    product

                    product_id
                    product_Name
                    product_Price
                    product_Description
                    product_image
                    category_id

another table      category
                   category_id
                   category_name

我有一个带有文本框的新表单

                                  txtprodname
                                  txtproddescrip
                                  txtproductprice
                                  picturebox1
                                  txtcategoryname

我正在尝试使用以下代码将新产品添加到产品表中

我正在使用实体框架..

     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();       

   }

我正在尝试将新产品添加到产品表中……但我不知道如何使用 linq 将类别名称添加到类别表中……

如何将类别名称添加到类别表并使用我输入的类别名称更新产品表...

如何使用输入的这个名称以及添加到产品表中的新产品更新产品表中的类别 ID

任何人都可以帮助解决这个问题......

我正在使用 winforms .....和 ​​c# 语言

这是我的实体图......

【问题讨论】:

    标签: c# winforms linq entity-framework linq-to-entities


    【解决方案1】:

    我假设您想首先检查输入的类别是否已经存在,如果存在则想要重用它。我还假设 tsgentity 是您的 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);
    

    上面的代码应该在你运行tsgentity.SaveChanges()之前执行。

    【讨论】:

    • 我有类别描述文本框我也需要为类别描述做同样的事情..
    • 这真的取决于你想要达到的目标......如果有人添加了一个产品,他真的想要更改/更新它的类别描述吗?询问准确的要求。
    • 嗨..我无法在此行 tsgentity.Categories.Add(category); 之前获得 pd.category...
    • 向我们展示您的 productcategory 课程。
    • 查看我的产品和类别类实体图
    【解决方案2】:

    如果您正确映射了所有内容,您应该可以执行以下操作:

    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();
    

    希望这会有所帮助。 :)

    【讨论】:

    • @hi abbas 是否有可能使用实体指定表的外键
    • 很抱歉,您的问题对我来说不是很清楚。你想达到什么目标?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多