entity-framework - Entity Framework 的 FromSql 方法执行使用内部连接选择语句的 SQL Server 表值函数
问题描述
我创建了实体类来匹配函数列,将 DbSet 包含在上下文类中,并完成了存储库中的方法,但是使用 FromSql 的函数调用不起作用。
这是代码:
SQL 服务器功能:
CREATE FUNCTION dbo.ufn_viewInvoices() RETURN TABLE AS RETURN (SELECT i.InvoiceId, i.CustomerId, c.CompanyName, c.LastName, a.StreetNumber, a.StreetName, a.City, i.Revenue, i.DateStarted, i.DateCompleted, i.DaysEstimate, i.DaysActual FROM Invoices i, Customers c, Addresses a WHERE i.CustomerId = c.CustomerId AND i.WorkAddressId = a.AddressId);
我的实体类与函数的 select 语句中的列相匹配:
public class InvoiceCustomerNameAddresses { [Key] public byte InvoiceId { get; set; } public byte CustomerId { get; set; } public string CompanyName { get; set; } public string LastName { get; set; } public string StreetNumber { get; set; } public string StretName { get; set; } public string City { get; set; } public decimal? Revenue { get; set; } public DateTime? DateStarted { get; set; } public DateTime? DateCompleted { get; set; } public byte? DaysEstimate { get; set; } public byte? DaysActual { get; set; } }
DbSet
课堂上的DbContext
:public DbSet<InvoiceCustomerNameAddresses> InvoiceCustomerNameAddresses { get; set; }
这是存储库中的方法:
public List<InvoiceCustomerNameAddresses> GetInvoicesUsingTVF() { List<InvoiceCustomerNameAddresses> invoicesList; try { Console.WriteLine("Inside GetInvoicesUsingTVF method in repo, about to call function"); invoicesList = context.InvoiceCustomerNameAddresses.FromSql("SELECT * FROM [dbo].ufn_viewInvoices()").ToList(); Console.WriteLine("Function called, invoicesList count: " + invoicesList.Count); } catch (Exception) { Console.WriteLine("In repo's catch, so something went wrong"); invoicesList = null; Console.WriteLine("An error occurred"); } return invoicesList; }
控制器中的方法:
[HttpGet] public List<InvoiceCustomerNameAddresses> GetAllInvoicesUsingTVF() { List<InvoiceCustomerNameAddresses> detailedInvoicesList = null; try { Console.WriteLine("Inside controller, about to call the repo's getInvoicesUsingTVF method"); detailedInvoicesList = invoiceRepository.GetInvoicesUsingTVF(); Console.WriteLine("Received detailedInvoicesList, count: " + detailedInvoicesList.Count); } catch (Exception) { Console.WriteLine("In catch block, so something went wrong"); detailedInvoicesList = null; } return detailedInvoicesList; }
请注意:
上面代码中的控制台打印输出语句,在使用方法调用函数之前和之后
.FromSql
:第一个控制台打印输出被执行,但是函数调用语句的执行存在一个明显的问题,因为下一个被执行的控制台打印输出是在 catch 块中的那个.请注意屏幕截图中显示的输出中显示“执行 ObjectResult,写入类型为‘null’的值”的行。这反映了 catch 块中的语句:“invoicesLIst = null”(见附图)。
请注意,此函数调用不使用参数,最后,从 SQL Server Management Studio 执行此函数可以正常工作。
问题:我错过了什么我的函数调用.FromSql
不起作用?
解决方案
我发布了这个问题,因为一段时间后,显而易见的事情似乎并不那么明显,多一双眼睛可能有助于看到我所缺少的东西。我没有得到异常,但没有得到我期望的数据(附加的输出截图),因为我有一个拼写错误。实体类中的属性必须与使用 EF 的 FromSql 方法调用的函数中的 select 语句的列名匹配。因此,无法识别上述代码中实体的“StretName”;它应该是“街名”。它现在工作得很好。
推荐阅读
- c# - GetAwaiter() 会抛出一个异常吗?
- json - 将 apiDefaults 的值设置为变量
- selenium - 如何在基于测试参数运行的 Selenium 中并行运行文件中的每个测试标签的日志
- c# - 将 WSDL 的类对象传递给显示空值的 Web 服务
- c# - 从form1单击按钮时从form2调用按钮
- wagtail - 如何在主页上添加联系表格
- python - 比较字典,更新不覆盖值
- java - Jboss 自动调用 DB Oracle 的 PL 程序
- javascript - 如何使用 Google Script 为大型数据集生成距离矩阵?
- wordpress - 域更改:使用 htaccess 重定向 301 不起作用