使用DbContext获取子计数并映射到属性

时间:2019-01-31 15:04:11

标签: entity-framework linq dbcontext

我有以下对象树,其中每个计划都是注释和关注的子项的父项。在客户端上,我真的不在乎90%的时间,因此在按学科进行扩展时,注释是延迟加载的。但是对于我正在使用的当前功能,我需要有关它们的一些元数据。

[DataContract]
public class Plan
{
    [Key]
    public int Id { get; set; }
    //Ton of properties

}

[DataContract]
public class Comment
{
    [Key]
    public int Id { get; set; }

    [DataMember]
    [Required]
    public bool IsConcern { get; set; }

    [DataMember]
    [Required]
    public bool IsResolved { get; set; }

    [DataMember]
    [MaxLength(30)]
    public string Discipline { get; set; }

    [DataMember]
    public int Plan_Id { get; set; }

    //Other properties
}

我想做的就是这样

[DataContract]
public class Plan
{
    [Key]
    public int Id { get; set; }
    //Ton of properties

    public int Discipline1UnresolvedConcernCount?  { get; set; }
    //... Discipline2.... Discipline 8


}


    modelBuilder.Entity<Plan>()
        .Ignore(x => x.IsIntegrated)
        .Ignore(x => x.Discipline1UnresolvedConcernCount)
        .Ignore(x => x.Discipline2UnresolvedConcernCount)

服务

public class PlansController : BaseODataController
{
    /// <summary>
    /// Look up a Plan record by ID
    /// </summary>
    /// <param name="key">ID for the Plan</param>
    [EnableQuery]
    [Authorize]
    public SingleResult<Plan> Get([FromODataUri] int key)
    {
        using (CreateTimedTelemetryTrace($"key:{key}"))
        {
            IQueryable<Plan> result = DataContext.Plans.Where(bu => bu.Id == key)
           //Improve Query to get counts


            return SingleResult.Create(result);
        }
    }
}

有什么方法可以做即时消息吗?

0 个答案:

没有答案