ado.net的问题 [英] Problem with ado.net
本文介绍了ado.net的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个经典的Ado.net实体,如客户和订单。我还有一个WPF窗口并使用Mvvm架构。我是mvvm和Ado.net的新手,所以我无法弄清楚如何获取我的datagrid(使用一个itemsource)显示来自clients表和orders表的项目。它可能听起来很明显,但问题是我的Orders属性(我设置为datagrid的ItemSource)没有来自clients表的项目。
我从db获取数据的方法:
订单:
I have a classic Ado.net entity like Clients and Orders. Also I have a WPF window and using Mvvm architecture. I am new to mvvm and Ado.net so I cant figure out how can get my datagrid(that using one itemsource) show items from clients table and orders table. Its might sound obvious but problem is taht my Orders property(that i set as ItemSource for datagrid) dont have items from clients table.
My methods to get data from db:
Orders:
public IEnumerable<Order> GetOrders()
{
using (var context = new SvLaserEntities())
{
var result = context.Orders.ToList();
result.ForEach(e => context.Detach(e));
return result;
}
}
客户:
Clients:
public IEnumerable<Client> GetClients()
{
using (var context = new SvLaserEntities())
{
var result = context.Clients.ToList();
result.ForEach(e => context.Detach(e));
return result;
}
}
推荐答案
数据库中是否连接了两个表?如果没有,您可能需要稍微重新设计您的数据库。通常,Orders表将具有某种ClientId,它是Clients表的主键和Orders表中的外键。如果您这样做,那么您可以使用以下内容从客户端表和相关订单中提取客户数据:
Are the two tables connected in your database at all? If not, you likely have to redesign your database slightly. Typically, an Orders table would have a ClientId of some sort, which is a primary key of the Clients table and a foreign key in the Orders table. If you did that, then you could pull the Clients data from the Client table and the relevant Orders using something like:
foreach(Client c in context.Clients)
{
// do something with c...
Order o=c.Orders;
// do something with o...
}
好运!
Best of luck!
这篇关于ado.net的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文