如何让实体框架使用生成的类上的接口?
我有一个项目,其中客户端正在使用实体框架,并且我试图从应用程序的其余部分中抽象出生成的类。
一个生成的类是 Category,它具有 Type 作为属性。
我已经创建了一个我想要 Category 实现的接口,如下所示:
public interface ICategory
{
string Type { get; set;}
}
我之前已经在 LINQ to SQL 中完成了此操作,并且运行良好。我在单独的文件中创建一个分部类并让它实现接口:
public partial class Category: ICategory
//implement interface
但是,每当我尝试使用 EF 构建查询时,它都会说它不支持 OfType<>()。
示例:
var query = from c in DataContext.Category
where Type == "some type"
select c;
var resultsList = query.OfType<ICategory>(); //error here (not supported)
我在这里做错了什么?
其他需要注意的事项:我正在 silverlight 应用程序中开发它,并且数据上下文实际上是从服务中提取的,因此这里也存在客户端服务器关系。
I have a project where the client is using Entity Framework, and I'm trying to abstract away the generated classes from the rest of the application.
One generated class is Category and it has say Type as a property.
I've created an interface that I want Category to implement, like this:
public interface ICategory
{
string Type { get; set;}
}
I have done this in LINQ to SQL before and it works fine. I create a partial class in a separate file and have it implement the interface:
public partial class Category: ICategory
//implement interface
However, with EF whenever I try to build a query with EF it says it doesn't support OfType<>().
Example:
var query = from c in DataContext.Category
where Type == "some type"
select c;
var resultsList = query.OfType<ICategory>(); //error here (not supported)
What am I doing wrong here?
Other things to note: I'm developing this in a silverlight application and the data context is actually being pulled from a service, so there's a client server relationship going on here as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一般规则,LINQ to Entities 只能理解实体模型 (EDMX) 的一部分。因此,虽然您可以自由地将实体类型扩展为分部类,但您不能使用在 LINQ to Entities 查询中添加的属性、方法和接口引用,某些非常具体的功能除外。
但是,在这种情况下,以下查询应该为您提供结果想:
As a general rule, LINQ to Entities can only understand things which are part of your entity model (EDMX). So while you are free to extend your entity types be a partial classes, you cannot use properties, methods, and interface references you add there in LINQ to Entities queries, except for certain, very specific features.
However, in this case the following query should give you the result you want: