实体框架-(主详细信息)如何更新与主表单分开的表单中的详细信息 [英] Entity Framework - (master detail) how to update Details which on a separate form from Master

查看:78
本文介绍了实体框架-(主详细信息)如何更新与主表单分开的表单中的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目使用WinForms进行实体框架数据绑定但是我没有使用单一表格,而是使用了两种表格.

I am trying to do this for my project Entity Framework Databinding with WinForms but instead of using single form, I'm using 2 forms.

CategoryDataGridView 显示在 Form1 上,然后在 Form1 上有一个编辑按钮,它将加载具有的Form2.> ProductsDataGridView (选定类别的产品列表)和一个保存按钮以保存更改

CategoryDataGridView is displayed on the Form1, then there is an edit button on Form1, which will load the Form2 which has the ProductsDataGridView (product list of selected category) and a save button to save the changes

我在Form2上有以下代码

I have the following code on my Form2

ProductContext _context = new ProductContext();
public int SeletedCategID { get; set; }

private void Form2_Load(object sender, EventArgs e)
{
    _context.Products.Where(c => c.CategoryId ==     SeletedCategID).ToList();
    productsBindingSource.DataSource =     (_context.Products.Local).ToList();
}

private void SaveBtn_Click(object sender, EventArgs e)
{
    this.Validate();

    foreach (var product in _context.Products.Local.ToList())
    {
        if (product.Category == null)
        {
            _context.Products.Remove(product);
        }
    }
    this._context.SaveChanges();

    this.productsDataGridView.Refresh();

    Form1 frm1 = (Form1)Application.OpenForms["Form1"];
    frm1.Activate();
    frm1.Refresh();
    this.Dispose();
}

我现在的问题是仅将产品上的编辑保存到数据库.添加和删除不会保存到数据库.

my problem now is only edits on the products are saved to DB. Add & Delete are not saved to database.

推荐答案

在设置 context.Products时,应将 context.Products.Local 设置为绑定源的数据源.Local.ToList().

You should set context.Products.Local as data source of the binding source, while you set context.Products.Local.ToList().

context.Products.Local 获得一个 ObservableCollection< Product> ,它将与上下文保持同步,因此所有实体的更改(添加/删除/编辑)都将被跟踪上下文.
当您使用 context.Products.Local.ToList()时,您正在使用的 List< T> 不会与上下文保持同步,并且添加/删除操作也不会已跟踪,并且在调用 SaveChanges 时,不会保存您的添加和删除,而将保存编辑.

context.Products.Local gets an ObservableCollection<Product> which will stay in sync with context and so all entities changes (add/remove/edit) will tracked by context.
When you use context.Products.Local.ToList() then you are using a List<T> which will not stay in sync with context and Add/Remove will not be tracked and when calling SaveChanges your Adds and Removes will not be saved, while Edits will be saved.

DbSet< TEntity> .Local 获取一个 ObservableCollection< T> ,它表示所有对象的本地视图此集中的已添加,未更改和已修改实体.此局部视图在从上下文中添加或删除实体时,它们将保持同步.同样,添加到本地视图或从本地视图中删除的实体将自动添加到上下文中或从上下文中删除.

DbSet<TEntity>.Local gets an ObservableCollection<T> that represents a local view of all Added, Unchanged, and Modified entities in this set. This local view will stay in sync as entities are added or removed from the context. Likewise, entities added to or removed from the local view will automatically be added to or removed from the context.

代码:

您可以通过以下方式加载数据:

You can load data this way:

context.Products.Where(x => x.CategoryId == SelectedCategoryId).ToList();
this.productBindingSource.DataSource = context.Products.Local;

您可以通过以下方式保存数据:

And you can save data this way:

this.Validate();
this.productBindingSource.EndEdit();
context.Products.Local
       .Where(x => x.CategoryId == 0)
       .ToList().ForEach(x =>
       {
           x.CategoryId = SelectedCategoryId;
       });
context.SaveChanges();
this.DialogResult = DialogResult.OK;

您还不需要删除具有空类别的产品的那部分代码,因为您没有使用master-detail.这是一个可编辑的列表.

You also don't need that part of code which removes products having null category, because you are not using master-detail. It's an editable list.

另外,请注意,在关闭第二种格式后,请不要忘记以第一种格式重新加载数据.由于您在表单中使用了不同的上下文,因此他们不知道其他上下文中的更改.

As another side-note don't forget to reload data in first form, after you closed second form. Since you are using different contexts in your forms, they are not aware of changes in other context.

这篇关于实体框架-(主详细信息)如何更新与主表单分开的表单中的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆