Wcf 如何在实体框架中仅显示表的特定列?
如何在实体框架中显示表的某些特定列而不是整个表Wcf 如何在实体框架中仅显示表的特定列?,wcf,entity-framework,Wcf,Entity Framework,如何在实体框架中显示表的某些特定列而不是整个表 using (DataEntities cxt = new DataEntities()) { notes note = cxt.notes.Where(no => no.id == accID).SingleOrDefault(); return notes; } 为此,我建议您使用ViewModel,如下所示:- notes note = cxt.notes.SingleOrDefault(no => no.id
using (DataEntities cxt = new DataEntities())
{
notes note = cxt.notes.Where(no => no.id == accID).SingleOrDefault();
return notes;
}
为此,我建议您使用ViewModel,如下所示:-
notes note = cxt.notes.SingleOrDefault(no => no.id == accID);
var model = new YourViewModel // Your viewModel class
{
ID = note.ID,
PropertyOne = note.PropertyOne, // your ViewModel Property
PropertyTwo = note.PropertyTwo
};
你可以用它来做这件事。
这意味着直接在XML中编辑您的模型,因为没有设计器对此的支持,但是您将得到一个独立实体,它的字段比原始实体少
优势:
- 然后,您可以直接查询数据库中这个被截断的实体(您只需要从数据库中获取所需的字段,而不需要从数据库中获取整个实体,并在代码中对其进行转换)
- 在希望将此截断实体发送到 客户 使用WCF将流量降至最低(例如,在客户端上构建大列表时 这基本上只需要名称和ID,而不需要其他特定实体 信息)
- 此基于QueryView的实体是只读的。要使其可写,您必须自己添加r/w功能