Say*_*emi 18 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
Run Code Online (Sandbox Code Playgroud)
我的函数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
Run Code Online (Sandbox Code Playgroud)
Gae*_*ael 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>
Run Code Online (Sandbox Code Playgroud)
使用Partial class机制创建新的源文件并将代码添加到自动生成的DbContext类(比如她的名字MyEntities)(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();
}
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
using (var context = new MyEntities())
{
int id = 1;
string culture = "fr-FR";
string result = null;
result = context.GetContentByIdAndCul(id, culture);
}
Run Code Online (Sandbox Code Playgroud)
直到今天,实体框架还没有支持生成标量函数的调用.但是,您可以解决在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();
}
}
Run Code Online (Sandbox Code Playgroud)
为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;
}
Run Code Online (Sandbox Code Playgroud)
确保你的功能已添加到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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13224 次 |
| 最近记录: |