过滤CTP5中的可空属性和其他EF4烦恼 [英] Filtering on nullable properties in CTP5 and other EF4 annoyances
问题描述
我周末将项目从Linq转换为SQL到EF4 Code First,大部分转换都进展顺利。
一个很大的烦恼就是实体Framework处理未知方法的能力有限。
使用Linq to SQL,我可以构建有用且灵活的查询,例如:
from x < span style ="color:Blue;"> in someObject
where !x.IsDeleted.GetValueOrDefault()
选择 new SomeViewModel
{
val = x.val,
anotherVal = x.AnotherVal.GetValueOrDefault(),
YetAnotherVal = SomeMethod(x),
};
Chris Chris,
这些是当前的限制EF。
1)您可以在可空字段上使用.HasValue和.Value属性。查询可以写成如下:
来自某个对象的x为
在哪里!x.IsDeleted.HasValue || !x.IsDeleted.Value
选择...
2)与上述类似,你需要使用.HasValue拼出逻辑。
anotherVal = x.AnotherVal.HasValue? x.AnotherVal:0
3)这不受支持,因为EF LINQ提供程序无法将SomeMethod(x)逻辑推送到数据库。为此,您需要将结果具体化为中间结果集,然后使用SomeMethod。另一种方法是让EF知道
如何使用模型定义函数处理数据库中SomeMethod的逻辑;
http:// thedatafarm .com / blog / data-access / ef4-model-defined-functions-level-1-amp-2 / 。
~Rowan
I've been converting a project from Linq to SQL to EF4 Code First over the weekend and most of the conversion has gone well.
The one big annoyance is Entity Framework's limited ability to deal with unknown methods.
With Linq to SQL I can build queries that are useful and flexible such as:
from x in someObject where !x.IsDeleted.GetValueOrDefault() select new SomeViewModel { val = x.val, anotherVal = x.AnotherVal.GetValueOrDefault(), YetAnotherVal = SomeMethod(x), };
Hi Chris,
These are current limitations of EF.
1) You can use the .HasValue and .Value properties on the nullable. The query can be written as follows:
from x in someObject
where !x.IsDeleted.HasValue || !x.IsDeleted.Value
select ...2) Similar to above you need to spell the logic out using .HasValue
anotherVal = x.AnotherVal.HasValue ? x.AnotherVal : 03) This isn't supported because the EF LINQ provider can't push the SomeMethod(x) logic to the database. To do this you would need to materialize the results to an interim result set and then use SomeMethod. Another alternative would be to let EF know how to process the logic of SomeMethod in the database using Model Defined Functions; http://thedatafarm.com/blog/data-access/ef4-model-defined-functions-level-1-amp-2/.
~Rowan
这篇关于过滤CTP5中的可空属性和其他EF4烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!