在 Entity Framework Code First 中添加关联实体计数的属性
我使用 Code First 编写数据层,然后使用 RIA 服务传输到 Silverlight 前端。由于我必须序列化所有内容,因此我想在通过网络发送之前获取有关每个实体的一些附加信息(以减少加载时间)。过去,我通过将所有内容转换为具有附加信息的 POCO 类来完成此操作。我想知道是否有更好的方法来做到这一点。为了给你一个想法,这是我的课程:
public class District
{
// ... Other properties, not important
public ICollection Installations { get; set; }
//The property I would like to calculate on the fly
[NotMapped]
public int InstallationCount { get; set; }
}
有没有办法让这个属性在我通过网络发送之前自动计算?一种选择是仅包含 Installation 集合,但这会增加大量数据(Installation 实体上大约有 50 个属性,每个区可能有数百条记录)。
I'm using Code First to write my data layer, then transmitting to a Silverlight front end using RIA services. Since I have to serialize everything, I would like to get some additional information on each entity before sending it across the wire (to reduce load time). In the past I have done this by translating everything to a POCO class that has the additional information. I'm wondering if there's a better way of doing this. To give you an idea, here's my class:
public class District
{
// ... Other properties, not important
public ICollection Installations { get; set; }
//The property I would like to calculate on the fly
[NotMapped]
public int InstallationCount { get; set; }
}
Is there a way to have this property calculate automatically before I send it across the wire? One option would be just to Include the Installation collection, but that adds a lot of bulk (there are about 50 properties on the Installation entity, and potentially hundreds of records per district).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需将 InstallationCount 设置为自动属性,只需使用 get 返回 Installations 集合的计数函数即可。
Rather than making InstallationCount an automatic property, just use the get to return the count function of Installations collection.