将手动的LINQ到SQL的映射与前pressions工作? [英] Will manual Linq-To-Sql mapping with Expressions work?

查看:199
本文介绍了将手动的LINQ到SQL的映射与前pressions工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的问题:

I have this problem:

类型派生的 EntityObject 键入它具有属性ID。

The Vehicle type derives from the EntityObject type which has the property "ID".

我想我知道为什么L2S不能把它理解SQL-不知道WHERE子句中应包括,其中VehicleId ==值 VehicleId BTW是餐桌上的PK,而在对象模型的属性,方法同上,是ID。

I think i get why L2S can't translate this into SQL- it does not know that the WHERE clause should include WHERE VehicleId == value. VehicleId btw is the PK on the table, whereas the property in the object model, as above, is "ID".

我甚至可以赢得这与前pression树?因为它似乎很容易创建一个防爆pression 传递给的SingleOrDefault 的方法,但会L2S仍然失败翻译呢?

Can i even win on this with an Expression tree? Because it seems easy enough to create an Expression to pass to the SingleOrDefault method but will L2S still fail to translate it?

我试图将DDD友好,所以我不希望装点着 Col​​umnAttributes 我的域模型对象等等。不过,我高兴地定制我L2S的dbml文件,并添加防爆pression佣工/不管在从我的域模型为止保持这个ORM企业的,希望我的数据层。

I'm trying to be DDD friendly so i dont want to decorate my domain model objects with ColumnAttributes etc. I am happy however to customise my L2S dbml file and add Expression helpers/whatever in my "data layer" in the hope of keeping this ORM-business far from my domain model.

更新:

我没有用在我的SELECT语句中的对象初始化语法。像这样的:

I'm not using the object initialization syntax in my select statement. Like this:

private IQueryable<Vehicle> Vehicles() 
{
    return from vehicle in _dc
        select new Vehicle() { ID = vehicle.VehicleId };
}

..我实际使用一个构造函数和从我读过这会导致上述问题。这是我在做什么:

.. i'm actually using a constructor and from what i've read this will cause the above problem. This is what i'm doing:

private IQueryable<Vehicle> Vehicles() 
{
    return from vehicle in _dc
        select new Vehicle(vehicle.VehicleId);
}

据我所知,L2S无法从截屏上面,因为它不知道它通常会从对象初始化语法推断的映射转换前pression树。我怎样才能解决这个?我是否需要建立一个防爆pression 带有属性的绑定?

推荐答案

我已决定,这是不可能的进一步的体验。

I have decided that this is not possible from further experience.

L2S根本无法创建WHERE子句,当一个参数构造函数用于映射预测正确的。它在传统的L2S映射预测的初始化语法,这使得L2S它所需要的内容。

L2S simply can not create the correct WHERE clause when a parameterized ctor is used in the mapping projection. It's the initializer syntax in conventional L2S mapping projections which gives L2S the context it needs.

答案很简单 - 使用NHibernate的

Short answer - use NHibernate.

这篇关于将手动的LINQ到SQL的映射与前pressions工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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