C# EF模型类-Linq选择为非数据库属性
我设计了下表,并使用EF 6对其进行了映射,因此当我尝试选择一个包含所有文档和每个文档的单独类型的公司时,EF将从ListCompanyDocumentsType返回嵌套数据。我试过一些方法——见下文 表格 1-不好-它从列表CompanyDocumentTypes加载公司、文档、文档类型和嵌套文档C# EF模型类-Linq选择为非数据库属性,c#,database,entity-framework,linq-to-entities,C#,Database,Entity Framework,Linq To Entities,我设计了下表,并使用EF 6对其进行了映射,因此当我尝试选择一个包含所有文档和每个文档的单独类型的公司时,EF将从ListCompanyDocumentsType返回嵌套数据。我试过一些方法——见下文 表格 1-不好-它从列表CompanyDocumentTypes加载公司、文档、文档类型和嵌套文档 public static IQueryable<Company> CompleteCompanies(micwayEntities context) {
public static IQueryable<Company> CompleteCompanies(micwayEntities context)
{
return context.Companies
.Include(l1 => l1.CompanyDocuments)
.Include(l2 => l2.CompanyDocuments.Select(p=> p.ListCompanyDocumentType));
}
3-不工作-当公司加载DB时会引发异常
此外,我尝试在CompanyDocument模型类中创建一个非映射属性,以尝试从CompanyDocument模型类中的“ListCompanyType”加载“Name”,但它引发了一个异常,因为EF没有加载此信息-请参见下文
我的问题是:
向上的任何人Tks
public static IQueryable<Company> CompleteCompanies(micwayEntities context)
{
return context.Companies.Include(l1 => l1.CompanyDocuments);
}
[NotMapped]
public string NondbTypeName
{
get
{
using (var db = new micwayEntities())
{
var listCompanyDocumentType = db.ListCompanyDocumentTypes.FirstOrDefault(p => p.id.Equals(typeId));
return listCompanyDocumentType?.name;
}
}
}
public static IQueryable<Company> CompleteCompanies(micwayEntities context)
{
return context.Companies.Include(l1 => l1.CompanyDocuments);
}
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; }
}