【发布时间】:2014-10-15 14:41:10
【问题描述】:
我已经为我的一些实体类实现了一个接口:
public partial class Order : IReportable
{
public string TableName { get { return "Order"; } }
}
public partial class Client: IReportable
{
public string TableName { get { return "Client"; } }
}
public interface IReportable
{
string TableName { get; }
}
然后我将它添加到 DbContext:
public virtual DbSet<IReportable> IReportable { get; set; }
当我尝试查询所有实现该接口的表时(如图所示):
var result = from reportabletable in db.IReportable
where reportabletable.TableName == table_name
select reportabletable
我得到以下异常:
未映射类型“Report.DataAccess.IReportable”。检查 没有使用 Ignore 方法明确排除该类型 或 NotMappedAttribute 数据注释。验证类型是 定义为一个类,不是原始的或泛型的,并且不继承 来自实体对象。
【问题讨论】:
-
如果很多 ORM 都提供这种类型的查询,我会感到惊讶
-
@MarcGravell 你是说不可能吗?
-
检查实体框架的继承策略。如果可能有 DbSet
,我不知道,但您可以使用基本抽象类来解决这个问题,也许可以使用虚拟(或抽象)属性 TableName。顺便提一句。有一个属性 TableName - 在此处检查继承策略的链接 - entityframeworktutorial.net/code-first/…
标签: c# asp.net-mvc linq entity-framework