C# C实体框架继承:自动将属性映射到子属性

C# C实体框架继承:自动将属性映射到子属性,c#,entity-framework,C#,Entity Framework,我想知道在从数据库检索时是否可以自动将属性映射到子属性 例如: public class Account { [Key] [Index(IsUnique = true)] public Guid ID { get; set; } = Guid.NewGuid(); [Required] [Index(IsUnique = true)] [StringLength(50)] public string Email { get; set; }

我想知道在从数据库检索时是否可以自动将属性映射到子属性

例如:

public class Account
{
    [Key]
    [Index(IsUnique = true)]
    public Guid ID { get; set; } = Guid.NewGuid();

    [Required]
    [Index(IsUnique = true)]
    [StringLength(50)]
    public string Email { get; set; }

    [Required]
    public string Password { get; set; }

    public string Hash { get; set; }

    public ICollection<Role> Roles { get; set; }

    [ForeignKey("AccountProfile")]
    public Guid? AccountProfileId { get; set; }

    public virtual AccountProfile AccountProfile { get; set; }
}

public class AccountProfile
{
    [Key]
    [Index(IsUnique = true)]
    public Guid ID { get; set; } = Guid.NewGuid();

    [Required]
    public string Name { get; set; }

    [Required]
    public string Phone { get; set; }

    public ICollection<AccountAddress> Addresses { get; set; }
}
从数据库检索数据时,我想将名称自动映射到base.AccountProfile.Name:

List<AccountVM> models = db.Accounts.Include(m => m.Roles).Include(m => m.AccountProfile).ToList();
还有我的猎犬:

List<AccountVM> models = db.Accounts.Include(m => m.Roles).Include(m => m.AccountProfile).Select(m => m as AccountVM).ToList();
然后出现另一个错误:

不支持输入类型为“Yountrep.Models.Account”且检查类型为“Yountrep.ViewModels.AccountVM”的“TypeAs”表达式。LINQ to Entities查询中仅支持实体类型和复杂类型


我想现在的问题是如何将其键入AccountVM

您的视图模型不应继承自实体模型。您可以像这样创建视图模型以包含任何关系对象

public class AccountVM
{
    public string Name { get; set; }
    public AccountProfile Profile { get; set; }

    public class AccountProfile
    {
        public string Name { get; set; }
    }
}
在生成LINQ查询时,您可以手动复制属性:

List<AccountVM> vm = db.Accounts.Select(a => 
{ 
    new AccountVM {
        Name= a.Name,
        Profile = new AccountProfile {
            Name = a.AccountProfile.Name
        }    
    }
}).ToList();
另一种选择是使用AutoMapper。我在我的几个项目中使用AutoMapper,它减少了手动翻译每个字段的需要

如果要展平特性,请执行以下操作:

    public class AccountVM
    {
        public string Name { get; set; }
        public string ProfileName { get; set; }
    }

List<AccountVM> vm = db.Accounts.Select(a => 
{ 
    new AccountVM {
        Name= a.Name,
        ProfileName = (a.AccountProfile == null) ? "" : a.AccountProfile.Name
       }    
    }
}).ToList();

我还添加了一个三元表达式来检查null。如果配置文件不存在,这将避免出现异常错误。

如果使用“选择所有内容”功能,则为“是”。有没有办法避免呢。我希望Account中的属性与AccountVM中的属性处于同一级别。在您的示例中,AccountVM必须将AccountProfile分配到另一个属性中,我希望避免它。代码效率和最小化代码之间存在差异。使用n层体系结构时,视图模型和实体模型应完全分开处理。映射模型属性是标准做法。它允许您获取实体模型并将其转换为客户机可以使用的东西。我曾经试图做你正试图做的事情,但发现它违反了单一责任原则和关注点分离。
List<AccountVM> vm = db.Accounts.Select(a => 
{ 
    new AccountVM {
        Name= a.Name,
        Profile = new AccountProfile {
            Name = a.AccountProfile.Name
        }    
    }
}).ToList();
    public class AccountVM
    {
        public string Name { get; set; }
        public string ProfileName { get; set; }
    }

List<AccountVM> vm = db.Accounts.Select(a => 
{ 
    new AccountVM {
        Name= a.Name,
        ProfileName = (a.AccountProfile == null) ? "" : a.AccountProfile.Name
       }    
    }
}).ToList();