代码之家  ›  专栏  ›  技术社区  ›  GWB

在不公开实体框架依赖项/实现详细信息的情况下使用EdmFunctionAttribute?

  •  5
  • GWB  · 技术社区  · 16 年前

    EdmFunctionAttribute 不引入对实体框架的依赖/系统.数据.实体.dll?

    我想我可以有一个方法的接口和一个具体的实现,用 EDM功能属性

    我有一个上下文接口 IMyContext 在程序集和实体框架实现中定义 MyContext 在另一个集会上。

    public interface IMyContext
    {
        double SomeFunction(double first, double second);
    
        // other interface details here
    }
    
    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.");
        }
    
        // rest of interface implementation here
    }
    

    using (IMyContext context = ContextFactory.GetNewContext())
    {
        var results = context.Table.Select(t => context.SomeFunction(t.Col1, t.Col2)).ToList();
    }
    

    NotSupportException 说LINQ to实体不能识别方法“Double SomeFunction(Double,Double)”。

    如果我把上下文投射到具体的实现中

    using (MyContext context = ContextFactory.GetNewContext() as MyContext)
    {
        ...
    }
    

    然后它开始工作,但是我需要指定具体的实现,我不想这样做。

    这个函数不必是context类的成员,我只是把它放在那里进行探索。

    2 回复  |  直到 16 年前
        1
  •  1
  •   Steven    14 年前

    Generic Repository Pattern IQueryable

    IQueryable FindAll(Func<T,bool> exp);
    
        2
  •  0
  •   Akash Kava    15 年前

    这是不可能的,因为Linq查询生成器将调查接口类型,而不是具体的类。表达式树将包含接口方法的方法引用,当您调查方法上的属性时,它不会返回任何属性。

    TypeReplacer tr = new TypeReplacer();
    tr.Visit(ex);
    
    class TypeReplacer: ExpressionVisitor{
        protected override MethodCallExpression MethodCall(MethodCallExpression exp)
        {
           // compare exp.Method and 
           // replace it with concrete Type's method
           return exp;
        }
    }