我设计了以下表格并使用EF 6对其进行了映射,因此当我尝试选择一个包含所有文档和每个文档的单独类型的公司时,EF会返回嵌套数据来自ListCompanyDocumentsTypes。我尝试过一些方法 - 见下文
表格 1 - 不好 - 它从ListCompanyDocumentTypes
加载公司,文档,文档类型和嵌套文档 public static IQueryable<Company> CompleteCompanies(micwayEntities context)
{
return context.Companies
.Include(l1 => l1.CompanyDocuments)
.Include(l2 => l2.CompanyDocuments.Select(p=> p.ListCompanyDocumentType));
}
数据返回
1) - 公司
2) - &#34;公司文件清单&#34;来自每家公司
3) - &#34; CompanyDocumentType&#34;对于每个CompanyDocument
4) - &#34; CompanyDocument&#34;对于每个CompanyDocumentType
2 - 工作 - 从选择中未映射
因为我只需要&#34; ListCompanyDocumentTypes&gt;名称&#34;,所以为了解决这个问题,我在CompanyDocument表中创建了一个NonMapped属性,以便从DB加载它 - 见下文;但是,我不确定这是否是正确的方法。
public static IQueryable<Company> CompleteCompanies(micwayEntities context)
{
return context.Companies.Include(l1 => l1.CompanyDocuments);
}
CompanyDocument.cs
[NotMapped]
public string NondbTypeName
{
get
{
using (var db = new micwayEntities())
{
var listCompanyDocumentType = db.ListCompanyDocumentTypes.FirstOrDefault(p => p.id.Equals(typeId));
return listCompanyDocumentType?.name;
}
}
}
3 - DOESNT WORK - 在公司加载DB时抛出异常
另外,我尝试在CompanyDocument模型类中创建一个Non-Mapped属性,以尝试加载&#34; Name&#34;来自&#34; ListCompanyType&#34;这是在CompanyDocument模型类中,但它引发了一个例外,因为EF没有加载这些信息 - 见下文
public static IQueryable<Company> CompleteCompanies(micwayEntities context)
{
return context.Companies.Include(l1 => l1.CompanyDocuments);
}
CompanyDocument.cs
public partial class CompanyDocument
{
#region NonMappedProperties
[NotMapped]
public string NondbTypeName => ListCompanyDocumentType.name;
#endregion
public int id { get; set; }
[Required]
[StringLength(15)]
public string companyAbn { get; set; }
public int typeId { get; set; }
[Required]
[StringLength(50)]
public string description { get; set; }
[StringLength(50)]
public string number { get; set; }
[Column(TypeName = "date")]
public DateTime? expiryDate { get; set; }
[Required]
[StringLength(255)]
public string displayName { get; set; }
[Required]
[StringLength(255)]
public string path { get; set; }
public bool isArchived { get; set; }
public bool isDeleted { get; set; }
public DateTime createdOn { get; set; }
[Required]
[StringLength(50)]
public string createdById { get; set; }
public DateTime? lastChangedOn { get; set; }
[StringLength(50)]
public string lastChangeById { get; set; }
public virtual Company Company { get; set; }
public virtual ListCompanyDocumentType ListCompanyDocumentType { get; set; }
public virtual Worker WorkerCreatedBy { get; set; }
public virtual Worker WorkerLastChangeBy { get; set; }
}
我的问题是: