如何在EF Core Power Tools中使用SQL Server Functions



我在我的asp.net web应用程序中使用EF Core Power Tools。在我的模型和过程中使用它很长一段时间后,我想将它用于我现有的数据库功能。我以与项目中的过程和模型相同的方式加载函数,当我想用以下代码调用函数时,我注意到生成的函数没有实现。我搜索了互联网,没有找到一种方法来使用我的SQL服务器的功能。有办法加载它与EF核心的电动工具吗?

如何调用函数

var x = ASB_ZentralContext.GET_UST_Text(customerId, articleId);

生成的函数代码

public partial class ZentralContext
{
[DbFunction("GET_UST_Satz", "dbo")]
public static double? GET_UST_Satz(int? Zahler_ID, int? Artikel_ID)
{
throw new NotSupportedException("This method can only be called from Entity Framework Core queries");
}
[DbFunction("GET_UST_Text", "dbo")]
public static string GET_UST_Text(int? Zahler_ID, int? Artikel_ID)
{
throw new NotSupportedException("This method can only be called from Entity Framework Core queries");
}
protected void OnModelCreatingGeneratedFunctions(ModelBuilder modelBuilder)
{
}
}

我试过这样做:

我在我的DbContext中添加了这个

public int Get_UST_Satz(int zahler_ID, int artikel_id)
=> throw new NotSupportedException();

和这个在我的dbContext ModelBuilder

modelBuilder.HasDbFunction(typeof(Context).GetMethod(nameof(Get_UST_Satz), new[] { typeof(int), typeof(int) }))
.HasName("GET_UST_Satz");

函数返回标量值,因此不能将其视为表。

查看EF Core文档和示例代码在Power Tools wiki

var result = db.Orders.Where(o => o.Reference == NorthWindContext.GetReference(o.OrderName)).ToList();

最新更新