实体框架获取链接的实体(更快的结果)

时间:2018-03-06 09:02:32

标签: c# asp.net-mvc entity-framework

我有一个SQL数据库,其中有几个链接在一起的表。

“项目”表:包含有关项目的信息

“发布”表:包含与项目相关的版本信息(一个项目 - >多个版本;一个版本 - >一个项目)

“IssuesSW”表:包含与版本相关的问题的信息(一个问题SW - >在多个版本中链接;一个版本 - >包含多个issueSW)

在我的第一个解决方案中,我刚刚返回了大约10.000个问题的完整列表,但是对于其他过滤选项(基于项目/版本,问题依赖于),我需要项目和版本中的其他数据。

第一个解决方案我花了大约2秒才完成了请求(以及前端的一些图表的绘制)已经完成但是我需要在项目级别上启动附加数据,直到找到问题并且这需要太长时间了。

在我目前的解决方案中,我只是使用foreach循环来投掷项目并找到他们的版本然后去发布版本并找到所有问题:

[HttpPost]
    public JsonResult GetReleasesWithIsws()
    {
        using (var db = new SwMetricsContext())
        {
            var result = new List<GetCustomerReleasesWithIswsResult>();

            var projects = db.Projects.ToList();
            foreach (var project in projects)
            {
                var releases = db.PVERs.Where(x => x.Project_ID == project.ID).ToList(); 
                foreach(var release in releases)
                {
                    var pver_result = new GetCustomerReleasesWithIswsResult()
                    {
                        Project_Id = project.ID,
                        Project_Title = project.Title,
                        Project_Generation = project.Generation,
                        Project_DefaultDevelopmentMethod = project.DefaultDevelopmentMethod,
                        Release_Id = release.ID,
                        Release_Category = release.Category,
                        Release_Classification = release.Classification,
                        Release_Department = project.ResponsibleAtBosch,
                        Release_ImplementationFreeze = release.ImplementationFreeze,
                        Release_LifeCycleState = release.LifeCycleState,
                        Release_PlannedDate = release.PlannedDate,
                        Release_SpecificationFreeze = release.SpecificationFreeze,
                        Release_Title = release.Title,
                        Release_Type = release.Type
                    };
                    var isw_connections = db.PVERIssuesSW.Where(x => x.Release_ID == release.ID).ToList().Select(x => x.IssueSW_ID).ToHashSet();
                    var isws = db.IssuesSW.Where(x => isw_connections.Contains(x.ID)).ToList();
                    pver_result.Release_Isws.AddRange(isws);

                    result.Add(pver_result);
                }
            }
            var json = Json(result);
            json.MaxJsonLength = int.MaxValue;
            return json;
        }
    }

但这个解决方案的问题是需要太长时间(大约30秒)。 所以,如果某人有更好的解决方案,我会非常感激:)

以下是3个数据库表模型:

[Table("DGSIT_SWMetrics_Projects")]
public class Project
{
    public string ID { get; set; }
    public string Title { get; set; }
    public int Customer_ID { get; set; }
    public string Responsible { get; set; }
    public string DefaultDevelopmentMethod { get; set; }
    public string Generation { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
    public string SpjmName { get; set; }
}

[Table("DGSIT_SWMetrics_PVERs")]
public class Pver  // Release 
{

    public string ID { get; set; }
    public string Project_ID { get; set; }
    public string Type { get; set; }
    public string Title { get; set; }
    public string PlannedDate { get; set; }
    public string ImplementationFreeze { get; set; }
    public string SpecificationFreeze { get; set; }
    public string LifeCycleState { get; set; }
    public string Category { get; set; }
    public string Classification { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
}

[Table("DGSIT_SWMetrics_IssuesSW")]
public class IssueSW
{
    public string Project_ID { get; set; }
    public string ID { get; set; }
    public string Title { get; set; }
    public string LCS { get; set; }
    public string Category { get; set; }
    public string DevelopmentMethod { get; set; }
    public string Allocation { get; set; }
    public string Tags { get; set; }
    public string SubmitDate { get; set; }
    public string ExternalReview { get; set; }
    public int Assignee_ID { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
}

IssueSW有自己的“Project_ID”,因为它取决于一个“Cluster-Project”。但它也与发布有额外的表格相关联:

[Table("DGSIT_SWMetrics_PVERIssuesSW")]
public class PVERIssueSw
{
    public int ID { get; set; }
    public string Release_ID { get; set; }
    public string IssueSW_ID { get; set; }
}

0 个答案:

没有答案