LinqToSql和抽象基类 [英] LinqToSql and abstract base classes

查看:148
本文介绍了LinqToSql和抽象基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个继承了这样的事情有些LINQ实体:

 公共抽象类EntityBase {公众诠释标识{搞定; }} 

公共接口IDeviceEntity {INT DEVICEID {搞定;组; }}

公共抽象类DeviceEntityBase:EntityBase,IDeviceEntity
{
公共抽象INT DEVICEID {搞定;组; }
}

公共部分类ActualLinqGeneratedEntity:DeviceEntityBase
{
}

在一个通用的方法,我查询DeviceEnityBase派生实体:

 返回unitOfWork.GetRepository< TEntity方式>()FindOne(X => x.DeviceId == evt.DeviceId); 

其中TEntity有一个约束实现的就是它DeviceEntityBase。该查询总是与消息类成员DeviceEntityBase.DeviceId是未映射一个InvalidOperationException失败。即使我添加了抽象基类中的一些映射信息以



  [列(存储=_DeviceId的DbType =诠释NAME =DEVICEID,IsDbGenerated =假,UpdateCheck的= UpdateCheck.Never)] 


解决方案

LINQ到SQL具有的部分的经鉴别支持继承(的这里,的这里),但你可以在LINQ模型中定义的类上只有查询 - 即数据类本身,以及(更重要的也许这个例子)查询本身必须在数据类的术语来表述:虽然TEntity是一个数据类,它知道该财产这里是关于实体基础声明



一个选项可能是动态表达式;它的类本身申报的财产(即失去了基类,但保留接口) - 但是这不是小事。



表达工作将类似下面,并指出,你可能想要么在字符串中作为参数传递,或者获得通过反射的主键(如果它归结):

 静态表达式来; Func键< T,BOOL>> BuildWhere< T>(INT DEVICEID){
VAR ID = Expression.Constant(DEVICEID的typeof(INT));
变种精氨酸= Expression.Parameter(typeof运算(T),×);
VAR道具= Expression.Property(ARG的DeviceID);
返回Expression.Lambda<&Func键LT; T,BOOL>>(
Expression.Equal(道具,ID),ARG);
}


I have some linq entities that inherit something like this:

public abstract class EntityBase { public int Identifier { get; } }

public interface IDeviceEntity { int DeviceId { get; set; } }

public abstract class DeviceEntityBase : EntityBase, IDeviceEntity
{
  public abstract int DeviceId { get; set; }
}

public partial class ActualLinqGeneratedEntity : DeviceEntityBase
{
}

In a generic method I am querying DeviceEnityBase derived entities with:

return unitOfWork.GetRepository<TEntity>().FindOne(x => x.DeviceId == evt.DeviceId);

where TEntity has a contraint that is it a DeviceEntityBase. This query is always failing with an InvalidOperationException with the message "Class member DeviceEntityBase.DeviceId is unmapped". Even if I add some mapping info in the abstract base class with

[Column(Storage = "_DeviceId", DbType = "Int", Name = "DeviceId", IsDbGenerated = false, UpdateCheck = UpdateCheck.Never)]

解决方案

LINQ-to-SQL has some support for inheritance via a discriminator (here, here), but you can only query on classes that are defined in the LINQ model - i.e. data classes themselves, and (more perhaps importantly for this example) the query itself must be phrased in terms of data classes: although TEntity is a data class, it knows that the property here is declared on the entity base.

One option might be dynamic expressions; it the classes themselves declared the property (i.e. lose the base class, but keep the interface) - but this isn't trivial.

The Expression work would be something like below, noting that you might want to either pass in the string as an argument, or obtain the primary key via reflection (if it is attributed):

static Expression<Func<T, bool>> BuildWhere<T>(int deviceId) {
    var id = Expression.Constant(deviceId, typeof(int));
    var arg = Expression.Parameter(typeof(T), "x");
    var prop = Expression.Property(arg, "DeviceId");
    return Expression.Lambda<Func<T, bool>>(
        Expression.Equal(prop, id), arg);
}

这篇关于LinqToSql和抽象基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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