开发者

Adding a property for the count of associated entities in Entity Framework Code First

开发者 https://www.devze.com 2023-04-11 11:11 出处:网络
I\'m using Code First to write my data lay开发者_StackOverflower, then transmitting to a Silverlight front end using RIA services.Since I have to serialize everything, I would like to get some additio

I'm using Code First to write my data lay开发者_StackOverflower, 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).


Rather than making InstallationCount an automatic property, just use the get to return the count function of Installations collection.

public class District
{
    public virtual ICollection<Installation> Installations { get; set; }

    [NotMapped]
    public int InstallationCount { get { return Installations.Count; } }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号