如何让实体框架5修改的对象列表 [英] How to get list of modified objects in Entity Framework 5

查看:265
本文介绍了如何让实体框架5修改的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我结合实体列表来这样一个数据网格视图:

  VAR订单= context.Order.ToList(); 

&的BindingList LT;排序> orderList =新的BindingList<排序>(订单);

dataGridView1.DataSource = orderList;

用户可以编辑或添加新的直接datagridview的。当用户点击保存按钮,以优化性能,我想检索已更改/新执行插入/更新实体的名单。我怎样才能做到这一点。



修改定义添加新行的GridView:

  BindinList<排序>订单=( - 的BindingList LT;排序>)dataGridView1.Datasource; 

order.Add(新订单());



编辑2 解决:

  BindinList<排序>订单=( - 的BindingList LT;排序>)dataGridView1.Datasource; 

订购订购=新订单();

context.Order.Add(订单);

order.Add(订单);


解决方案

 列表<对象> modifiedOrAddedEntities = context.ChangeTracker.Entries()
。凡(X => x.State == System.Data.EntityState.Modified
|| x.State == System.Data.EntityState.Added )
。选择(X => x.Entity).ToList();

在EF实体绑定到 DataGridView的它往往是最好创建一个 IBindingList的 DbSet.Local 的ObservableCollection 。这样,你得到双向绑定,并通过当添加新的实体将被自动添加到上下文BindingSource.Add() IBindingList.Add() 。最简单的方法来得到这个工作,一旦正确绑定,是设置 DataGridView.AllowUserToAddRows 对用户输入将被添加到上下文新的实体真实和新行。

  context.Orders.Load(); 
&的BindingList LT;排序>的BindingList = context.Orders.Local.ToBindingList();
的BindingSource ordersBindingSource =新的BindingSource();
ordersBindingSource.DataSource =的BindingList;
dataGridView1.DataSource = ordersBindingSource;



System.Data.Entity的必须引用使用 .ToBindingList(),你必须使用EF4.1或更大。


I'm binding list of entities to a data grid view like this:

var orders = context.Order.ToList();

BindingList<Order> orderList = new BindingList<Order>(orders);

dataGridView1.DataSource = orderList;

User can edit or add new directly on datagridview. When user click Save button, in order to optimize performance, I want to retrieve list of entities that has been changed/new to perform insert/update. How can I achieve this?

EDIT Define add new row to gridview:

BindinList<Order> orders = (BindingList<Order>)dataGridView1.Datasource;

order.Add(new Order());

EDIT 2 Solve:

BindinList<Order> orders = (BindingList<Order>)dataGridView1.Datasource;

Order order = new Order();

context.Order.Add(order);

order.Add(order);

解决方案

List<Object> modifiedOrAddedEntities = context.ChangeTracker.Entries()
 .Where(x => x.State == System.Data.EntityState.Modified 
        || x.State == System.Data.EntityState.Added)
 .Select(x=>x.Entity).ToList();

When binding EF entities to a DataGridView it is often preferable to create an IBindingList from the DbSet.Local ObservableCollection. This way you get two way databinding and your new entities are automatically added to the context when adding via BindingSource.Add() or IBindingList.Add(). The simplest way to get this working, once properly bound, is to set DataGridView.AllowUserToAddRows to true and new rows the users enter will be new entities Added to the context.

context.Orders.Load();
BindingList<Order> bindingList = context.Orders.Local.ToBindingList();
BindingSource ordersBindingSource = new BindingSource();
ordersBindingSource.DataSource = bindingList;
dataGridView1.DataSource = ordersBindingSource ;

System.Data.Entity must be referenced to use .ToBindingList() and you must be using EF4.1 or greater.

这篇关于如何让实体框架5修改的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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