我与其他实体有多对多关系来管理关联。
实体如下:
public class WorkOrderItem : EntityBase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long WorkOrderItemId { get; set; }
public virtual ICollection<WorkOrderItemLanguage> WorkOrderItemLanguages { get; set; }
}
public class WorkOrderItemLanguage
{
[Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.None)]
public long WorkOrderItemId { get; set; }
[Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.None)]
public string LanguageId { get; set; }
[Key, Column(Order = 2), DatabaseGenerated(DatabaseGeneratedOption.None)]
public LanguageType LanguageType { get; set; }
public virtual WorkOrderItem WorkOrderItem { get; set; }
public virtual Language Language { get; set; }
}
public class Language : EntityBase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string LanguageId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Code { get; set; }
public string IsoCode { get; set; }
public int Ordinal { get; set; }
public virtual ICollection<WorkOrderItemLanguage> WorkOrderItemLanguages { get; set; }
}
模型构建器配置
public class WorkOrderItemConfiguration : EntityTypeConfiguration<WorkOrderItem>
{
public WorkOrderItemConfiguration()
{
HasMany(x => x.WorkOrderItemLanguages).WithRequired(x => x.WorkOrderItem).HasForeignKey(x => x.WorkOrderItemId);
}
}
public class LanguageConfiguration : EntityTypeConfiguration<Language>
{
public LanguageConfiguration()
{
HasMany(x => x.WorkOrderItemLanguages).WithRequired(x => x.Language).HasForeignKey(x => x.LanguageId);
}
}
如果生成查询以选择包含与其相关的所有语言的WorkOrderItem,我如何包含语言实体?
以下查询未加载语言实体
UnitOfWork.Set<WorkOrderItem>()
.Include(x => x.WorkOrderItemLanguages.Select(l=>l.Language))
.FirstOrDefault(x.WorkOrderItemId == id);
答案 0 :(得分:2)
试试这个
var TesteEntities = UnitOfWork.Set<WorkOrderItem>()
.Include("WorkOrderItemLanguages.Language")
.FirstOrDefault(x.WorkOrderItemId == id);
你会在这里找到语言