EF model class - Linq select non database property

I designed these below tables and I mapped it using EF 6, so when I try to select one company with all documents and a separate type for each document, EF returns the nested data from ListCompanyDocumentsTypes. I've tried some methods - see below

Tables 1 - NOT GOOD - loads company, documents, documents and attachments from ListCompanyDocumentTypesenter image description here

    public static IQueryable<Company> CompleteCompanies(micwayEntities context)
    {
        return context.Companies
            .Include(l1 => l1.CompanyDocuments)
            .Include(l2 => l2.CompanyDocuments.Select(p=> p.ListCompanyDocumentType));
    }

      

DATA RETURNED

1) - Company

2) - "CompanyDocument List" from each company

3) - "CompanyDocumentType" for each CompanyDocument

4) - "CompanyDocument" for each CompanyDocumentType

2 - WORKS - not displayed with Select

Since I just need "ListCompanyDocumentTypes> Name", so to solve this problem, I created a NonMapped property on the CompanyDocument table to load from the DB - see below; however I am not sure if this is the correct way to do it.

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 - it throws an exception when the company loads the DB

Also, I tried to create a Non-Mapped property on the CompanyDocument model class to try to load "Name" from the "ListCompanyType" which is in the CompanyDocument model class, but it throws an exception because EF doesn't use 't to load this information - see below

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; }
}

      

My questions:

  • Is Approach 2 Correct?
  • Or is there a way to restrict nested objects via Include / Select as described in Approach 1?
  • Or can I force approach 3 to load the Name property without loading all nested objects?
+3


source to share





All Articles