实体框架4.0 - 将网格数据绑定到子表 [英] Entity Framework 4.0 - Databinding a grid to child tables

查看:52
本文介绍了实体框架4.0 - 将网格数据绑定到子表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个具有主记录和多个子记录的应用程序。 一个很好的例子是带有相关产品列表的订单。   我希望能够修改子记录(产品)
并能够将更改保存到数据库。

I am trying to create an application which will have a primary record and several child records.  A good example of this would be an Order with a list of associated Products.    I want to be able to modify the child records (Products) and be able to save the changes to the database.

要使问题复杂化,我需要将程序拆分为三层(用户界面,业务逻辑,数据访问)。    我希望BL和UI完全不知道数据存储。  此方法与主表一起正常工作。
(订单)。   当我尝试使用子表时,问题就开始了。 

To complicate the issue, I need the program split into three layers (UI, Business Logic, Data Access).     I want the BL and UI to be completely unaware of the data store.   This method is working fine with the primary table. (Order).    The problem starts when I attempt to work with the child table. 

我想使用POCO课程。   我已成功绑定了Orders的结果,但是在网格上查找数据似乎不会影响BusinessLayer中的数据实例。  UI上的Order对象和关联的Product
对象确实反映了这些更改。 这意味着我已经正确地进行了数据绑定设置,但我怀疑它是在复制数据而不是传递对象图的引用。

I want to use POCO classes.    I have successfully bound the results of Orders, however chaning the data on the grid does not appear to affect the instance of the data in the BusinessLayer.  The Order object and the associated Product objects on the UI do reflect the changes.  This implies I've got my databinding setup properly, but I suspect its making a clone of the data rather than passing a reference of the object graph.

我的代码是这样的:

UI Layer

订单O = OrdersBusinessObject.Read(OrderID);
$
this.orderBindingSource.DataSource =  O;

this.productsBindingSource.DataSource = O.Products;

Order O = OrdersBusinessObject.Read(OrderID);
this.orderBindingSource.DataSource = O;
this.productsBindingSource.DataSource = O.Products;

BL Layer

此图层创建DA的实例图层并使用它来访问数据。 此图层还会在将其返回到UI之前获取对正在读取的订单的引用

This layer creates an instance of the the DA layer and uses it to access the data.  This layer also grabs a reference to the Order being read before returning it to the UI

构造函数

DA = OrdersDataAccess()

DA = OrdersDataAccess()

读取 - 方法接受ID并返回订单对象图

Read - method accepts an ID and returns an Order object graph

CurrentOrder =  DA.Read(OrderID);

返回CurrentOrder;

CurrentOrder = DA.Read(OrderID);
return CurrentOrder;

验证

此方法适用于上述读取方法中检索到的CurrentOrder对象。

This method works with the CurrentOrder object that was retreived in the read method above.

Save - 此方法会将保存请求传递给数据访问层

Save - this method will pass the request for save on to the Data Access layer

DA.Save();

DA.Save();

数据访问

Data Access

构造函数

context =
new
OrderContext (ConnStr);

context = new OrderContext(ConnStr);

读取 - 此方法接受一个I​​D并返回新创建的对象

Read - this method accepts an ID and returns the newly created object

_Order = context.Orders.Where(r => r.OrderID == OrderID).FirstOrDefault< Order>();

context.LoadProperty(_Order,r => r.Products);

_Order = context.Orders.Where(r => r.OrderID == OrderID).FirstOrDefault<Order>();
context.LoadProperty(_Order, r => r.Products);

 

有任何想法吗? 

我不能成为唯一一个使用此方法的人,我们将非常感谢任何见解。

I can not be the only one using this method, any insight would be greatly appreciated.

 

谢谢

Dan

 

 

 

 

 

 

 

 

 

推荐答案

您好,

由于您想在Entity Framework中使用POCO并更新数据库中的数据,我建议您查看以下内容:

http:// msdn。 microsoft.com/en-us/library/dd456853.aspx

Since you want to use POCO in Entity Framework and update data in database, I would suggest you take a look at the following:
http://msdn.microsoft.com/en-us/library/dd456853.aspx

从我的角度来看,你发布的代码片段似乎没问题d。你能指出问题出在哪里吗?你能告诉我们你是如何将表格绑定到网格的吗?

From my perspective, it seems fine for the code snippet you posted. Can you point out where the problem occurs? Can you show us how did you bind the table to the grid?

祝你好运


这篇关于实体框架4.0 - 将网格数据绑定到子表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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