I have a situation where I would like to use a single business logic class to perform similar operations on a variety of entity framework classes. I have defined an interface which these classes implement in a partial class file.
However when I try to write a LINQ to entities query against these interface methods I get a NotSupportedException since the query is not using the class's properties directly but through an interface.
I would like to keep the heavy lifting to the database tier so is there a way to achieve this without resorting to LINQ to objects?
Here is some code which demonstrates my problem (it is using a generic repository class created by a factory).
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();
}
}