Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF模型类-Linq选择为非数据库属性_C#_Database_Entity Framework_Linq To Entities - Fatal编程技术网

C# EF模型类-Linq选择为非数据库属性

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) {

我设计了下表,并使用EF 6对其进行了映射,因此当我尝试选择一个包含所有文档和每个文档的单独类型的公司时,EF将从ListCompanyDocumentsType返回嵌套数据。我试过一些方法——见下文

表格 1-不好-它从列表CompanyDocumentTypes加载公司、文档、文档类型和嵌套文档

    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没有加载此信息-请参见下文

我的问题是:

  • 方法2正确吗
  • 或者是否有任何方法可以通过方法1所述的包含/选择来限制嵌套对象
  • 或者我可以强制方法3加载Name属性而不必加载所有嵌套对象吗

  • 向上的任何人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; }
    }