Winform中的实体框架

时间:2016-12-16 01:51:04

标签: c# winforms entity-framework

我在Winform中使用了 EF 。但是,我意识到我一直在以各种形式重复CRUD。所以,我试图创建一个基本表单,以便它可以被其他人继承。我使用User Control作为我的基本表单。 我想知道如何创建基本表单。下面是一些示例代码,我必须在每次创建新表单时以不同的形式创建保存。

    private void btnSave_Click(object sender, EventArgs e)
    {
        int ProductID = Convert.ToInt32(lblProductId.Text);

        using (var context = new OrderDbContext())
        {
            if (ProductID == 0)
            {
                if (!IsValidProductName(tbxProductName.Text))
                {
                    MessageBox.Show("Product Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Product products = new Product()
                {
                    ProductName = tbxProductName.Text,
                    ProductPrice = Convert.ToDecimal(tbxProductPrice.Text)
                };

                context.Products.Add(products);
            }

            else
            {
                var ProductToUpdate = context.Products.SingleOrDefault(Product => Product.ProductID == ProductID);
                if (ProductToUpdate != null)
                {
                    ProductToUpdate.ProductPrice = Convert.ToDecimal(tbxProductPrice.Text);
                    ProductToUpdate.ProductName = tbxProductName.Text;
                }
            }

            context.SaveChanges();
        }

        MessageBox.Show("Product Saved Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        tbxProductName.Text = "";
        tbxProductPrice.Text = "";
        InitializeProductList();
    }


    private void btnSave_Click(object sender, EventArgs e)
    {
        int SellerID = Convert.ToInt32(lblSellerId.Text);

        using (var context = new OrderDbContext())
        {
            if (SellerID == 0)
            {
                if (!IsValidMobileNumber(tbxSellerMobile.Text))
                {
                    MessageBox.Show("Seller Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Seller Sellers = new Seller()
                {
                    SellerName = tbxSellerName.Text,
                    SellerMobile = tbxSellerMobile.Text

                };

                context.Sellers.Add(Sellers);
            }

            else
            {
                var SellerToUpdate = context.Sellers.SingleOrDefault(Seller => Seller.SellerID == SellerID);
                if (SellerToUpdate != null)
                {
                    SellerToUpdate.SellerMobile = tbxSellerMobile.Text;
                    SellerToUpdate.SellerName = tbxSellerName.Text;
                }
            }

            context.SaveChanges();
        }

        MessageBox.Show("Seller Saved Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        tbxSellerName.Text = "";
        tbxSellerMobile.Text = "";
        InitializeSellerList();
    }

0 个答案:

没有答案