我将 Asp.Net WebApi Odata V4 与 Entity Framework 6 结合使用。 我正在尝试扩展派生类的导航属性,但出现以下错误。
基础实体
public abstract class BaseEntity
{
[ForeignKey("CreatedUser")]
public string CreatedBy { get; set; }
public virtual User CreatedUser { get; set; }
}
实体
public class Book: BaseEntity
{
public int BookId {get;set;}
}
Odata 模型生成器
builder.EntityType<BaseEntity>();
builder.EntitySet<Book>("Books");
数据查询
http://localhost/svc/Books(1)?$expand=CreatedUser
错误:
The 'TypeAs' expression with an input of type 'App.Models.Book' and a check of type 'App.Models.BaseEntity' is not supported. Only entity types and complex types are supported in LINQ to Entities queries.
我怎样才能克服这个问题?
请您参考如下方法:
对于您的模型构建器,不要将 BaseEntity
注册为 EntityType,而是使用具体类型:
builder.EntityType<Book>();
builder.EntitySet<Book>("Books");