错误6046:无法生成存储功能的函数导入返回类型

时间:2014-06-11 10:48:39

标签: linq entity-framework sql-function

我的sql数据库中有一个标量值函数。

将此功能导入Entity Framework模型时收到此错误:

Error 6046: Unable to generate function import return type of the store function 'GetContentByIdAndCul'.
The store function will be ignored and the function import will not be generated.   ..\EntityModels.edmx

我的函数tsql是:

ALTER FUNCTION [FRM].[GetContentByIdAndCul] 
(@Id int,@Culture nvarchar(5))
RETURNS nvarchar(max)
AS
BEGIN
declare @Result nvarchar(max)

if @Id is not null
    set @Result='This Content not defined in this Language'

select @Result=Value from CUL.Contents
WHERE ID=@Id AND (CUL.Contents.Culture = LOWER(@Culture) 
            OR CUL.Contents.Culture = LOWER(SUBSTRING(@Culture,1,2)))   
return @Result      
END

3 个答案:

答案 0 :(得分:15)

前面的答案显示了解决问题的好方法,但在现实生活中都没有。

这是一个经过测试的Entity Framework 6解决方案,对我有用。所以它应该适合你。

导入标量值函数

将标量值函数[FRM].[GetContentByIdAndCul]导入Entity Framework模型。它会自动在EntityModels.edmx文件的存储模型中创建相应的条目:

<Function Name="GetContentByIdAndCul" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="FRM" ReturnType="nvarchar(max)">
      <Parameter Name="Id" Type="int" Mode="In" />
      <Parameter Name="Culture" Type="nvarchar(5)" Mode="In" />
</Function>

添加代码以包含对标量值函数的调用

使用部分类机制(https://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.120%29.aspx)创建新的源文件并将代码添加到自动生成的DbContext类(例如,她的名字为MyEntities

public partial class MyEntities
{
    [DbFunction("EntityModels.Store", "GetContentByIdAndCul")]
    public string GetContentByIdAndCul(int id, string culture)
    {
       var objectContext = ((IObjectContextAdapter)this).ObjectContext;

       var parameters = new List<ObjectParameter>();
       parameters.Add(new ObjectParameter("Id", id));
       parameters.Add(new ObjectParameter("Culture", culture));

       return objectContext.CreateQuery<string>("EntityModels.Store.GetContentByIdAndCul(@Id, @Culture)", parameters.ToArray())
            .Execute(MergeOption.NoTracking)
            .FirstOrDefault();
    }
}

使用标量值函数

客户代码:

using (var context = new MyEntities())
{
    int id = 1;
    string culture = "fr-FR";
    string result = null;

    result = context.GetContentByIdAndCul(id, culture);
}

答案 1 :(得分:6)

直到今天,实体框架不支持为标量函数生成调用。 但是,您可以解决在DbContext类中编写这样的自定义方法的问题:

public partial class YouDbContext
{
    [DbFunction("YouDbContext.Store", "YourScalarFunction")]
    public string YourScalarFunction(string parameter)
    {
        var lObjectContext = ((IObjectContextAdapter)this).ObjectContext;

        return lObjectContext.
            CreateQuery<string >(
                "YouDbContext.Store.YourScalarFunction", 
                new ObjectParameter("parameterName", parameter)).
            Execute(MergeOption.NoTracking).
            FirstOrDefault();
    }
}

答案 2 :(得分:5)

为YOURMODEL.Context.cs创建一个分部类:public partial class YOUREntities : DbContext

[DbFunction("YOURModel.Store", "YOURSCALARFUNCTION")]
        public string YOURSCALARFUNCTION(string PARAMETER)
        {
            List<ObjectParameter> parameters = new List<ObjectParameter>(3);
            parameters.Add(new ObjectParameter("PARAMETER", PARAMETER));
            var lObjectContext = ((IObjectContextAdapter)this).ObjectContext;
            var output = lObjectContext.
                 CreateQuery<string>("YOURMODEL.Store.YOURSCALARFUNCTION(@PARAMETER)", parameters.ToArray())
                .Execute(MergeOption.NoTracking)
                .FirstOrDefault();
            return output;
        }

请确保您的功能已添加到YOURMODEL.EDMX,应该是这样的:

<Function Name="YOURSCALARFUNCTION" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" ReturnType="nvarchar(max)">
          <Parameter Name="PARAMETER" Type="nvarchar(max)" Mode="In" />
        </Function>