实体框架Linq很多关系不包括在select中

时间:2016-04-28 13:08:14

标签: c# entity-framework linq

我有以下实体:

public class EmployeeCv : UserEntity
{
        public byte ProfileImage { get; set; }
        public virtual List<Header> Headers { get; set; }
        [Index]
        public virtual List<ProjectExperience> ProjectExperiences { get; set; }
        [Index]
        public virtual List<Tag> Tags { get; set; }
}

public class Tag : AuditableEntity<int>
{
        public string Title { get; set; }
        public virtual List<EmployeeCv> EmployeeCv { get; set; }

        public virtual TagType TagType { get; set; }
}

public class TagType : AuditableEntity<int>
{
        public string Title { get; set; }
        public string CssClass { get; set; }
        public virtual List<Tag> Tags { get; set; }
}

我创建的DTO类只继承了实际的EmployeeCvEmployee类,因为Entity Framework不允许我在select中使用相同的类。

public class EmployeeCvDTO : EmployeeCv
{
}

public class EmployeeDTO : Employee
{
}

我的LINQ查询:

var listOfEmployeeCV = from cvs in _context.EmployeeCvs.Include(x => x.Employee).Include(x => x.Tags).Include(x => x.Tags.Select(y => y.TagType))
                                   .Include(x => x.ProjectExperiences)
                                   where
                              _context.Employees.Any(
                                    e => e.FirstName.Contains(SearchQuery.Keyword)
                                    && e.Id == cvs.Id
                              ) || (_context.Tags.Any(t => t.Title.Contains(SearchQuery.Keyword) && cvs.Tags.Contains(t)))
                                || (_context.ProjectExperiences.Any(pe => (pe.ProjectTitle.Contains(SearchQuery.Keyword)
                                || pe.Description.Contains(SearchQuery.Keyword)) &&
                                cvs.ProjectExperiences.Contains(pe)))
                                   select new
                                   EmployeeCvDTO
                                   {
                                       Employee = new EmployeeDTO
                                       {
                                           FirstName = cvs.Employee.FirstName,
                                           LastName = cvs.Employee.LastName,
                                           EnterpriseId = cvs.Employee.EnterpriseId,
                                           Level = cvs.Employee.Level
                                           //ProfileImageData = x.Employee.ProfileImageData
                                       },
                                       Tags = cvs.Tags,

                                       ProjectExperiences = cvs.ProjectExperiences,
                               };

上述查询完全符合我的要求。但是listOfEmployeeCV.Tags.TagType对于所有标记都为空。即使我已将其包含在.include语句中,但EmployeeCv -> Tags -> TagType始终为null。

EmployeeCvTags具有多对多关系,而TagsTagType具有一对多关系。 另外,添加以下LINQ也可以正常工作并返回tagType:

var lista = from cvs in _context.EmployeeCvs.Include(x => x.Employee).Include(x => x.Tags).Include(x => x.Tags.Select(y => y.TagType))
                                   .Include(x => x.ProjectExperiences) select cvs;
var listb = lista.ToList();

0 个答案:

没有答案