错误6046:无法生成存储函数的函数导入返回类型 [英] Error 6046: Unable to generate function import return type of the store function

查看:998
本文介绍了错误6046:无法生成存储函数的函数导入返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Entity Framework模型中导入此函数时,我收到此错误:

 错误6046:无法生成存储函数GetContentByIdAndCul的函数导入返回类型。 
存储函数将被忽略,不会生成函数导入。我的功能tsql是:

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

如果@Id不为null
set @ Result ='此内容未在此语言中定义'

select @ Result =来自CUL.Contents的值
WHERE ID = @ Id AND(CUL.Contents.Culture = LOWER(@Culture)
OR CUL.Contents.Culture = LOWER(SUBSTRING(@ Culture,1,2)))
return @Result
END


解决方案

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



这是一个经过验证的解决方案,具有适用于我的实体框架6



导入您的标量值函数



导入您的标量值函数 [FRM]。[GetContentByIdAndCul] 到您的实体框架模型。它会自动在您的 EntityModels.edmx 文件的存储模型中创建相应的条目:

 < Function Name =GetContentByIdAndCulAggregate =falseBuiltIn =falseNiladicFunction =falseIsComposable =trueParameterTypeSemantics =AllowImplicitConversionSchema =FRMReturnType =nvarchar(max)> ; 
< Parameter Name =IdType =intMode =In/>
< Parameter Name =CultureType =nvarchar(5)Mode =In/>
< / Function>



添加代码以包装调用您的标量值函数



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

  public部分类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);
}


I have a scalar-valued function in my sql database.

I receive this error when importing this function into Entity Framework model:

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

my function tsql is:

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

解决方案

Preceding answers show the good way to solve the problem but none works in real life.

Here's a tested solution with Entity Framework 6 that works for me. So it should works for you.

Import your scalar valued function

Import your scalar valued function [FRM].[GetContentByIdAndCul] into your Entity Framework model. It automatically creates corresponding entry in the storage model of your EntityModels.edmx file :

<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>

Add code to wrap call to your scalar valued function

Create new source file and add code to auto generated DbContext class (say her name is MyEntities) using Partial class mechanism (https://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.120%29.aspx)

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();
    }
}

Use your scalar valued function

Client code :

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

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

这篇关于错误6046:无法生成存储函数的函数导入返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆