我不明白为什么DbSet<> .OfType<>为过滤器操作抛出无效的元数据类型

时间:2012-10-10 00:21:20

标签: entity-framework-4

我有以下无法提供信息(在我看来)错误消息:

  

System.NotSupportedException未被用户代码

处理      

Message ='Company.Data.DataProvider.Schema.DerivedType'不是类型过滤操作的有效元数据类型。类型过滤仅对实体类型和复杂类型有效。

我的数据库中有以下表格:

BaseType
ObjectID uniqueidentifier
.. other unrelated columns

DerivedType
ObjectID unqueidentifier
UserID unqueidentifier
.. other unrelated columns

我有使用继承的实体框架使用的以下类:

BaseType
Guid ObjectID 
.. other unrelated properties

DerivedType : BaseType
Guid UserID 
.. other unrelated properties

My Partial SchemaContext如下所示:

public partial class SchemaContext
{
  public IQueryable<DerivedType> DerivedTypes
  {
    get { return this.BaseType.OfType<DerivedType>()
  }
}

我的方法如下:

result = this._dbContexts.SchemaContext.DerivedTypes
  .Include(i => i.RatingType)
  .Include(i => i.ObjectVersion)
  .Where(dt => objectIDs.Any(id => 
                 dt.ObjectVersion.
                   .MatchingObjects
                   .Select(s => s.ObjectID).Contains(id))
               && dt.UserID == userID)
  .ToList();

我相信.OfType<>抛出错误,我不能使用基类型,因为我需要过滤UserID。有关解决这个问题的任何建议吗?

1 个答案:

答案 0 :(得分:2)

我相信此错误消息表明您的派生类型未在实体模型中正确定义。如果使用操作实体对象类型名称的POCO / Context生成器,请验证对象的名称是否匹配。