本文介绍了LINQ,在映射中实现接口和异常的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将存储库模式与LINQ一起使用,具有IRepository.DeleteOnSubmit(T Entity).它工作正常,但是当我的实体类具有接口时,如下所示:

I'm using repository pattern with LINQ, have IRepository.DeleteOnSubmit(T Entity). It works fine, but when my entity class has interface, like this:

public interface IEntity { int ID {get;set;} }

public partial class MyEntity: IEntity {

    public int ID {
        get { return this.IDfield; }
        set { this.IDfield=value;  }
    }
}

,然后尝试删除这样的某些实体:

and then trying to delete some entity like this:

IEntity ie=repository.GetByID(1);
repoitory.DeleteOnSubmit(ie);

投掷
成员"IEntity.ID"不支持对SQL的转换.

throws
The member 'IEntity.ID' has no supported translation to SQL.

从数据库中获取数据有效,但删除和插入无效.如何针对DataContext使用接口?

fetching data from DB works, but delete and insert doesn't. How to use interface against DataContext?

这里是:
异常消息:成员'MMRI.DAL.ITag.idContent'没有支持的SQL转换.

Here it is:
Exception message:The member 'MMRI.DAL.ITag.idContent' has no supported translation to SQL.

代码:

var d = repContent.GetAll().Where(x => x.idContent.Equals(idContent));
foreach (var tagConnect in d)    <- error line
{
    repContet.DeleteOnSubmit(tagConnect);

(它从数据库中获取所有标签,并删除它们)

(it gets all tags from DB, and deletes them)

和堆栈跟踪:

[NotSupportedException: The member 'MMRI.DAL.ITag.idContent' has no supported translation to SQL.]
   System.Data.Linq.SqlClient.Visitor.VisitMember(SqlMember m) +621763
   System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +541
   System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) +8
   System.Data.Linq.SqlClient.SqlVisitor.VisitBinaryOperator(SqlBinary bo) +18
   System.Data.Linq.SqlClient.Visitor.VisitBinaryOperator(SqlBinary bo) +18
   System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +196
   System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) +8
   System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select) +46
   System.Data.Linq.SqlClient.Visitor.VisitSelect(SqlSelect select) +20
   System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +1024
   System.Data.Linq.SqlClient.SqlProvider.BuildQuery( ...

当我尝试装饰部分类时:

When I try do decorate partial class:

[Column(Storage = "_idEvent", DbType = "Int NOT NULL", IsPrimaryKey = true)]
public int idContent
{ get { return this.idEvent; } set { this.idEvent=value; } }

它引发错误无效的列名'idContent'."

it throws error "Invalid column name 'idContent'."

推荐答案

当在MVC4中使用linq-to-sql时,Microsoft似乎放弃了对接口中的==运算符的支持(或者可能从未得到支持).但是,您可以使用i.ID.Equals(someId)代替==运算符.

It appears Microsoft dropped support for == operator in interfaces when using linq-to-sql in MVC4 (or maybe it was never supported). You can however use i.ID.Equals(someId) in place of the == operator.

IQueryable铸造到IEnumerable可以,但是不应该使用!!原因是:IQueryable具有IEnumerable的时髦实现.无论您通过IEnumerable接口在IQueryable上使用哪种linq方法,都会导致首先执行查询,将所有结果从DB提取到内存中,并最终在数据上本地运行该方法(通常是那些方法将转换为SQL并在数据库中执行).想象一下,试图从包含数十亿行的表中获取一行,然后只提取所有行以选择其中的一个(而且,由于不小心将IQueryable强制转换为IEnumerable并延迟加载相关数据,这种情况会变得更糟).

Casting IQueryable to IEnumerable works but should not be used! The reason is: IQueryable has funky implementation of IEnumerable. Whatever linq method you'll use on a IQueryable through the IEnumerable interface will cause the query to be executed first, have all the results fetched to the memory from the DB and eventually running the method localy on the data (normally those methods would be translated to SQL and executed in the DB). Imagine trying to get a single row from a table containing billion rows, fetching all of them only to pick one (and it gets much worse with careless casting of IQueryable to IEnumerable and lazy loading related data).

显然,Linq在将==运算符与本地数据的接口一起使用时没有问题(因此仅影响IQueryable),也与实体框架(或我听说的)没有问题.

Apparently Linq has no problem using == operator with interfaces on local data (so only IQueryable is affected) and also with Entity Frameworks (or so I heard).

这篇关于LINQ,在映射中实现接口和异常的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:32