【问题标题】:Entity Framework Core to include different properties from subclassesEntity Framework Core 包含来自子类的不同属性
【发布时间】:2017-03-09 20:30:58
【问题描述】:

我的类和存储库的结构:

public class Group{
    //fields
}

public class User{
    public UserRole Role {get;set;}
}

public abstract class UserRole{
    //fields
}

public class PersonUserRole:UserRole{
    public Group Group {get;set;}
}

public class ManagerUserRole:UserRole{
    public IList<Group> Groups {get;set;}
}

我遇到问题的一个例子:

public class UserRepository:IUserRepository{
    private readonly ApplicationDbContext _dbContext;
    private readonly DbSet<User> _users;

    public UserRepository(ApplicationDbContext dbcontext)
    {
        _dbContext = dbcontext;
        _users = _dbContext.DomainUsers;
    }

    public User GetBy(int id)
        {
            User type = _users.Include(u => u.Role)
                              .SingleOrDefault(c => c.UserId == id);
            if (typeof(PersonUserRole) == type.Role.GetType())
            {
                return
                    _users.Include(u => u.Role)
                        .ThenInclude(r => (r as PersonUserRole).Groep)
                        .SingleOrDefault(c => c.UserId == id);
            }
            else
            {
                return _users.Include(u => u.Role)
                             .ThenInclude(r => (r as ManagerUserRole).Groups)
                             .SingleOrDefault(c => c.UserId == id);
            }
        }
}

我收到以下错误消息:

消息“属性表达式 'r => (r As PersonUserRole).Group' 不是 有效的。该表达式应表示属性访问:'t => t.MyProperty'

似乎我无法将我的 UserRole 类型转换为实际的 PersonUserRole 类型以包含 Group/Groups 属性。如何包含子类的属性?

【问题讨论】:

  • 如果你尝试 .ThenInclude(r => ((PersonUserRole)r).Group) 会怎样?
  • @Mangist 恐怕这会返回“值不能为空。参数名称:来源”错误。但我似乎找不到问题所在。
  • 如果您的对象无法转换为 PersonUserRole,则为 null。我个人会在没有 LINQ 的情况下这样做

标签: c# asp.net entity-framework-core


【解决方案1】:

更新:从版本 2.1 开始,EF Core 现在自然地支持 Include on derived types,通过 lambda as 运算符或 Include / ThenInclude 重载或 string @ 987654328@过载。

原始答案(EF Core 2.1 之前):

目前不支持急切加载派生实体导航属性。

作为一种解决方法,您可以结合使用 Eager loading、Explicit loading 和 Querying related entity,详见 Loading Related Data 部分EF Core 文档:

var user = _users.Include(u => u.Role).SingleOrDefault(u => u.UserId == id);
var userRoleQuery = db.Entry(user).Reference(u => u.Role).Query();
if (user.Role is ManagerUserRole)
    userRoleQuery.OfType<ManagerUserRole>().Include(r => r.Groups).Load();
else if (user.Role is PersonUserRole)
    userRoleQuery.OfType<PersonUserRole>().Include(r => r.Group).Load();
return user;

【讨论】:

【解决方案2】:

EntityFramework 有一个未解决的问题,与您的问题完全相同:https://github.com/aspnet/EntityFramework/issues/3910

也许您可以尝试他们的建议,但如果可行,我无法尝试:

var o = context.Set<Order>()
    .Where(o => o.Id == 1234)
    .Single();

context.Set<GroupPosition>()
    .Where(x => x.Order == o)
    .Include(x => x.GroupType)
    .Load();

context.Set<SalesPosition>()
    .Where(x => x.Order == o)
    .Include(x => x.Group)
    .Load();

【讨论】:

  • 当请求多个实体时,此解决方案(高度)优于接受的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2017-03-24
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多