Entity framework 如何仅检索基类(实体框架)?

Entity framework 如何仅检索基类(实体框架)?,entity-framework,Entity Framework,我在这里挠头已经有一段时间了。。。 我有一个Consumer类和一个继承Consumer的BillableConsumer类。他们都是消费者群体的一部分。问题在于此查询包含以下查询: Consumer consumer = (from c in _ctx.Consumers where c.ID = id select c).First(); 返回一个BillableConsumer实例!与此查询相同: BillableConsumer bconsumer = (from c in _ctx.

我在这里挠头已经有一段时间了。。。 我有一个Consumer类和一个继承Consumer的BillableConsumer类。他们都是消费者群体的一部分。问题在于此查询包含以下查询:

Consumer consumer = (from c in _ctx.Consumers where c.ID = id select c).First();
返回一个BillableConsumer实例!与此查询相同:

BillableConsumer bconsumer = (from c in _ctx.Consumers.OfType<BillableConsumer>() where c.ID = id select c).First();
BillableConsumer bconsumer=(从_ctx.Consumers.OfType()中的c开始,其中c.ID=ID选择c);

如何仅返回基类的实例?(这些是数据存储中的独立表)。

是的,这有点棘手

但不久前我做了一个调查

希望这有帮助


Alex

我猜尝试这样做违反了Liskov替换原则(请参见)可能是BillableConsumer不是一种消费者,您应该使用公共字段和两个继承者创建ConsumerBase:Consumer和BillableConsumer。为了完整性,我建议在您的回答中发布代码示例,并指向您自己的博客了解更多信息。