2010-11-15 21 views
0

Je rencontre un problème qui appelle une fonction définie par l'utilisateur à partir d'une requête LINQ. J'utilise le framework .NET 4.0 et VS 2010. Voici un instantané du XML pour la définition de la fonction edmx.Erreur lors de l'appel d'une fonction UDF à partir de la requête LINQ Entity Framework

<Schema Namespace="MystoreDWModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"> 
    <Function Name="RegularPrice" store:Name="RegularPrice" IsComposable="true" Schema ="MystoreDWExtension.mystore" Aggregate="false" BuiltIn="false" ReturnType="decimal" StoreFunctionName="fn_GetPrice"> 
     <Parameter Name="ProductID" Type="varchar" Scale="40" Mode="In"/> 
     <Parameter Name="PriceTypeID" Type="varchar" Scale="10" Mode="In"/> 
     <Parameter Name="GroupCode" Type="varchar" Scale="10" Mode="In"/> 
     <Parameter Name="GroupValue" Type="varchar" Scale="10" Mode="In"/> 
     <Parameter Name="EffectiveDate" Type="datetime" Mode="In"/> 
    </Function> 

je le talon de fonction suivante définie dans le code ...

[EdmFunction("MystoreDWModel.Store", "RegularPrice")] 
public decimal? RegularPrice(
    string ProductID, 
    string PriceTypeID, 
    string GroupCode, 
    string GroupValue, 
    DateTime EffectiveDate) 
{ 
    throw new NotImplementedException("You can only call this method as part of a LINQ expression"); 
} 

L'appel que j'utilise pour accéder à la fonction est la suivante ...

MystoreDWEntities4 test = new MystoreDWEntities4(); 

var prices = (from products in test.Products 
       select RegularPrice("PRODUCTID", "R", "D", "20", DateTime.Now)); 

Lorsque j'essaie d'accéder aux données de prix, je reçois l'erreur suivante ...

The specified method 
'System.Nullable`1[System.Decimal] RegularPrice 
(System.String, System.String, System.String, System.String, System.DateTime)' 
on the type 'EntityFrameworkTest.Form1' cannot be translated 
into a LINQ to Entities store expression because the instance 
over which it is invoked is not the ObjectContext over which 
the query in which it is used is evaluated. 

J'ai essayé plusieurs variantes de la configuration et appel en vain. Est-ce que quelqu'un a rencontré cette erreur avant et quelles mesures pourrais-je utiliser pour le réparer?

+0

Avez-vous essayé Entity SQL, comme Julie Lerman recommande dans ce post: http://thedatafarm.com/blog/data-access/calling-udfs-from-entity-framework-not-what- vous-pourriez-avoir-prévu /? – Devart

+0

@Devart oui .... ce qui précède est le résultat de cela .... il n'a pas fonctionné hors de la porte, donc je l'avais déconné avec et je ne pouvais pas l'obtenir de coopérer malheureusement. –

Répondre

3

Essayez de placer la méthode RegularPrice dans la définition de la classe MystoreDWEntities4 (utiliser la déclaration partielle comme dans l'exemple suivant):

public partial class MystoreDWEntities4 { 
    [EdmFunction("MystoreDWModel.Store", "RegularPrice")] 
    public decimal? RegularPrice(
    string ProductID, 
    string PriceTypeID, 
    string GroupCode, 
    string GroupValue, 
    DateTime EffectiveDate) 
    { 
    throw new NotImplementedException("You can only call this method as part of a LINQ expression"); 
    } 
} 

appeler ensuite comme la méthode d'instance ObjectContext, comme ici:

ObjectSet<Products> products = test.Products; 
var prices = from prod in products 
      select new { Price = test.RegularPrice("PRODUCTID", "R", "D", "20", DateTime.Now)};