无法从1到多个表

时间:2016-06-24 15:32:33

标签: c# entity-framework-6

我试图在C#中使用Entity Framework 6.1来获取两个表中的所有行。这是我的模型的摘录: enter image description here 目的是从SQL Server数据库填充List<Task>Task已自动生成:

public partial class Task
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Task()
    {
        this.Rotas = new HashSet<Rota>();
        this.PeopleOnTasks = new HashSet<PeopleOnTask>();
        this.TaskRotaSaves = new HashSet<TaskRotaSave>();
    }

public int TaskID { get; set; }
public string TaskName { get; set; }
public string Frequency { get; set; }
public string RotaResultsPath { get; set; }
public bool Active { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Rota> Rotas { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PeopleOnTask> PeopleOnTasks { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TaskRotaSave> TaskRotaSaves { get; set; }

}

我尝试了很多东西。最近的尝试是:

var t2 = dbContext.Tasks.Include("PeopleOnTasks");

显然生成了正确的SQL。当我在SSMS中运行SQL时,我看到每个任务至少有3个PeopleOnTask记录;但在申请中,我只得到一个。我试图迭代PeopleOnTask集合,以消除任何延迟加载混乱。

此查询获取正确的数据(以及其他一些内容),但它不是我可以使用的格式。

var rawData = dbContext.PeopleOnTasks.Include("Task");

这个SQL完全可以完成工作。我最后的办法是在存储过程中使用它。

SELECT t.TaskID, t.TaskName, t.Frequency, t.RotaResultsPath, t.Active, p1.PersonID, p1.PersonName, 
tm.TeamID, te.TeamName, p2.PersonID AS TeamPersonID, 
p2.PersonName AS TeamPersonName, pt.CopyOnly,
CASE WHEN EXISTS(SELECT 1 
                    FROM Note N
                    WHERE N.ForeignKey = pt.TaskID and N.ForeignTable = 'Task')
        THEN '1'
        ELSE '0'
END AS HasNote

FROM PeopleOnTask AS pt 
INNER JOIN Task AS t ON t.TaskID = pt.TaskID 
LEFT OUTER JOIN Person AS p1 ON pt.PeopleID = p1.PersonID and pt.PeopleType = 'p'
LEFT OUTER JOIN TeamMember AS tm ON pt.PeopleID = tm.TeamID and pt.PeopleType = 't'
LEFT OUTER JOIN Team AS te ON te.TeamID = tm.TeamID 
LEFT OUTER JOIN Person AS p2 ON tm.PersonID = p2.PersonID 
ORDER BY t.TaskName

是否可以修复第一次尝试,或者是否需要编写大量代码来重新格式化第二次尝试?

我并不担心此阶段的Note和其他链接,只是返回了正确的PeopleOnTask记录。

感谢。

在Ben Jones回答之后编辑

此尝试也失败了。引发异常“无法在LINQ to Entities查询中构造实体或复杂类型'NameSpace.Task'。”,HResult = -2146233067。

    var t2 = dbContext.PeopleOnTasks.Include("Task").Select(x => new Task
    {
        TaskID = x.TaskID,
        TaskName = x.Task.TaskName,
        Active = x.Task.Active,
        Frequency = x.Task.Frequency,
        RotaResultsPath = x.Task.RotaResultsPath,
        PeopleOnTasks = x.Task.PeopleOnTasks,
        TaskRotaSaves = x.Task.TaskRotaSaves
    });

1 个答案:

答案 0 :(得分:0)

如果这个有效

var rawData = dbContext.PeopleOnTasks.Include("Task");

为什么不将它解析为你想要的对象

var rawData = dbContext.PeopleOnTasks.Include("Task").Select(x => new insertclass {
 property = x.prop
});