我有一个情况,希望使用单个业务逻辑类来执行各种实体框架类的类似操作。我已经在部分类文件中定义了一个这些类实现的接口。
但是,当我尝试针对这些接口方法编写LINQ to entities查询时,由于查询未直接使用类的属性而是通过接口进行访问,因此会引发NotSupportedException异常。
我希望将大部分工作留给数据库层,那么有没有一种方法可以实现这一点而不必借助LINQ to objects呢?
以下是演示我的问题的一些代码(它使用一个由工厂创建的通用存储库类)。
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();
}
}