gpt4 book ai didi

linq - 错误 6046 : Unable to generate function import return type of the store function

转载 作者:行者123 更新时间:2023-12-02 05:39:26 29 4
gpt4 key购买 nike

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

最佳答案

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

这是一个经过测试的使用 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);
}

关于linq - 错误 6046 : Unable to generate function import return type of the store function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24161005/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com