EF Linq to entities query generating UNION for TPC CTP5 code-first entity

依然范特西╮ 提交于 2019-12-05 14:56:45

HappyUsers are Users. Hence, db.Users should return both. The EF is correct, here.

However, the EF does have a limitation: There is no way to (in L2E, anyway) return results of only one type. Due to the Liskov Substitution Principal, you don't generally want to do this. But if you do, you can do it in ESQL, and there are (somewhat tedious) workarounds for L2E.

Bottom line: If you find yourself wanting to do this, rethink your design. Inheritance is probably not the right relationship in this case.

@Craig Stuntz is right, since both types are related, EF will keep that relation intact.

If you want to decouple both User types, then you could use an abstract base class.

public abstract class AbstractUser
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class User : AbstractUser
{
}

public class HappyUser : AbstractUser
{
    public bool IsHappy { get; set; }
}

EF will now treat both entities as seperate and there's no need to call MapInheritedProperties() anymore.

Your select statements would then look like:

var users = db.Users.Where(...);
var happyUsers = db.HappyUsers.Where(...);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!