实体框架铸造问题 [英] EntityFramework Casting issues

查看:141
本文介绍了实体框架铸造问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LinqKit的PredicateBuilder构建我的查询。
它是伟大的,做我正在寻找的。



为了使我的代码更可重用(表和视图),我创建了一个泛型谓词构建器类:

  public class LocalPredicateBuilder< T>其中T:IResort 
...
var predicate = PredicateBuilder.True< T>(

它暴露了BuildPredicate方法,我可以这样使用:

  var predicate = new LocalPredicateBuilder< Resort>() .BuildPredicate(); 
var resort = _entities.Resorts.Where(predicate).ToList();

但是,当我尝试这样做时,我得到这个运行时错误(btw实体对象实现IResort):
无法将类型ConsoleApplication1.Entities.Resort转换为ConsoleApplication1.Entities .IResort'。LINQ to Entities只支持投射实体数据模型原始类型



我试过投射(没有工作):

  var rlist = eq.Cast< Resort>()。ToList(); 


更新

>

没有很多运气得到谓词使用界面工作,所以我通过使用POCO解决我的问题。

解决方案

嗯,错误是准确的。您不能在L2E查询中执行此操作,因为您的界面( IReport )不是您的实体模型的一部分,因此无法转换为SQL。你必须使用一个实体类型,而不是一个接口。


I am building my query using PredicateBuilder from LinqKit. it is great and does exactly what i am looking for.

To make my code more reusable (tables and views) i created a generic predicate builder class:

public class LocalPredicateBuilder<T> where T : IResort
...
    var predicate = PredicateBuilder.True<T>(

which exposes BuildPredicate method. I can use it like this:

var predicate = new LocalPredicateBuilder<Resort>().BuildPredicate();
var resorts = _entities.Resorts.Where(predicate).ToList();

however when i try to do this, i get this runtime error (btw entity objects implement IResort): Unable to cast the type 'ConsoleApplication1.Entities.Resort' to type 'ConsoleApplication1.Entities.IResort'. LINQ to Entities only supports casting Entity Data Model primitive types

i tried casting (didn't work):

var rlist = eq.Cast<Resort>().ToList();

Any other way i can get around this casting issue?

UPDATE

not having much luck getting predicates to work using interfaces.. so i solved my problem by going with POCOs.

解决方案

Well, the error is accurate. You can't do that in an L2E query, because your interface (IReport) is not part of your entity model and hence can't be converted to SQL. You have to use an entity type, not an interface for that.

这篇关于实体框架铸造问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆