首页 > 解决方案 > Entity Framework 的 FromSql 方法执行使用内部连接选择语句的 SQL Server 表值函数

问题描述

输出截图

我创建了实体类来匹配函数列,将 DbSet 包含在上下文类中,并完成了存储库中的方法,但是使用 FromSql 的函数调用不起作用。

这是代码:

  1. 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);
    
  2. 我的实体类与函数的 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; }
     }
    
  3. DbSet课堂上的DbContext

     public  DbSet<InvoiceCustomerNameAddresses> InvoiceCustomerNameAddresses { get; set; }
    
  4. 这是存储库中的方法:

     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;
     }
    
  5. 控制器中的方法:

     [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不起作用?

标签: entity-frameworkuser-defined-functions

解决方案


我发布了这个问题,因为一段时间后,显而易见的事情似乎并不那么明显,多一双眼睛可能有助于看到我所缺少的东西。我没有得到异常,但没有得到我期望的数据(附加的输出截图),因为我有一个拼写错误。实体类中的属性必须与使用 EF 的 FromSql 方法调用的函数中的 select 语句的列名匹配。因此,无法识别上述代码中实体的“StretName”;它应该是“街名”。它现在工作得很好。


推荐阅读