我有一种情况,我想使用单个业务逻辑类来对各种实体框架类执行类似的操作。我已经定义了这些类在部分类文件中实现的接口。通过接口属性LINQ to Entities
然而,当我尝试写一个LINQ到实体查询针对这些接口中的方法,因为查询不直接,但通过一个接口使用类的属性,我得到一个NotSupportedException异常。
我想繁重存到数据库层等是有办法做到这一点,而不诉诸LINQ的对象?
下面是一些代码,演示了我的问题(它使用的由工厂创建了一个通用存储库类)。
public interface INamedEntity
{
int ID { get; set; }
string Name { get; set; }
}
// This is an Entity Framework class which has CustomerID and CustomerName properties.
public partial class Customer: INamedEntity
{
int INamedEntity.ID
{
get { return this.CustomerID; }
set { this.CustomerID = value; }
}
string INamedEntity.Name
{
get { return this.CustomerName; }
set { this.CustomerName = value; }
}
}
...
public string GetName<T>(int entityID) where T: EntityObject, INamedEntity
{
using(var repository = RepositoryFactory.CreateRepository<T>())
{
return repository
.Where(e => e.ID == entityID)
.Select(e.Name)
.Single();
}
}
是的,这看起来像问题。 – gareththegeek 2012-02-17 10:50:41