Is there a good way to use EdmFunctionAttributeEntity Framework / System.Data.Entity.dll dependency without introducing dependency?
I thought that I could have an interface with a method and a specific implementation that implements a method using EdmFunctionAttributeto map it to a database function.
I have a contextual interface IMyContextdefined in an assembly and an Entity Framework implementation MyContextin another assembly.
public interface IMyContext
{
double SomeFunction(double first, double second);
}
public partial class MyContext : IMyContext
{
[EdmFunction("MyNamespace", "MyDatabaseFunction")]
public double SomeFunction(double first, double second)
{
throw new NotSupportedException("This method may only be called as part of a LINQ expression.");
}
}
I use factory (using StructureMap behind the scenes) to get a context instance as an interface type:
using (IMyContext context = ContextFactory.GetNewContext())
{
var results = context.Table.Select(t => context.SomeFunction(t.Col1, t.Col2)).ToList();
}
This calls a NotSupportException, saying that LINQ to Entities does not recognize the "Double SomeFunction (Double, Double)" method.
using (MyContext context = ContextFactory.GetNewContext() as MyContext)
{
...
}
, , .
, .