Why would I want to use an ExpressionVisitor?

本文介绍了一种使用ExpressionVisitor解决EntityFramework无法调用转换方法的问题。在一个应用中,数据库字段包含0或1,而应用需要使用布尔值。通过创建一个Flag对象并使用ExpressionVisitor在发送到EF前修改属性访问,成功解决了转换问题。

Why would I want to use an ExpressionVisitor?

There was a issue where on the database we had fields which contained 0 or 1 (numeric), and we wanted to use bools on the application.

The solution was to create a "Flag" object, which contained the 0 or 1 and had a conversion to bool.

We used it like a bool through all the application, but when we used it in a .Where() clause the EntityFramework complained that it is unable to call the conversion method.

So we used a expression visitor to change all property accesses like .Where(x => x.Property) to .Where(x => x.Property.Value == 1) just before sending the tree to EF.

 

 

 

转载于:https://www.cnblogs.com/chucklu/p/11580266.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值