[英]Call Scalar function from c#
我在sql数据库中有一个标量函数,我想用linq调用Entity上下文。
虽然我知道,您可以使用以下语法调用表函数。
[DbFunction("Context", "TableFunc")]
public virtual IQueryable<ModelEntity> TableFunc(int
Id)
{
var IdParam = new ObjectParameter("Id", typeof(int))
{
Value = Id
};
return (this as IObjectContextAdapter).ObjectContext
.CreateQuery<ModelEntity>(
"Context.TableFunc(@Id)",IdParam);
}
但是,如果我在这个地方使用标量函数。
我收到错误“未指定name参数”
我的标量函数没有任何参数。
更新:标量函数调用
[DbFunction("Context","GetValue")]
public virtual IQueryable<short> GetValue()
{
return (this as
IObjectContextAdapter).ObjectContext.CreateQuery<short>
("Context.GetValue()");
}
我最近通过查看GitHub SourceCode以及此链接找到了解决方案。 幸运的是,这个方法很简单,它只是没有用易读的方式编码,因此,我发现我很难正确使用它。
public partial class MainDbContext : DbContext
{
//....
[DbFunction("CodeFirstDatabaseSchema", "IsAssignedIncludedInPickedRoles")]
public static bool IsAssignedIncludedInPickedRoles(string pickedRoles, string assignedRoles)
{
throw new NotSupportedException();
}
//.....
}
在我使用“CodeFirstDatabaseSchema”字符串本身的义务中出现了歧义,尽管它是文档的一部分,引导您使用dbContext名称或包含dbContext的命名空间,但这些都不是第二件事就是没有代码你必须在函数内部写,你只需要通过种子方法中的SqlCommand创建SQL函数,然后享受你的工作
var rslt = await ctx.AssignedInstitutions.Where(e => MainDbContext.IsAssignedIncludedInPickedRoles(e.Roles, "ARIOB")).ToListAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.