简体   繁体   English

具有读取存储过程的实体框架

[英]Entity Framework With Read Stored Procedure

I am using EF 6 for a project and I am new to EF. 我正在为项目使用EF 6,但我是EF的新手。 For this particular project, we used an existing database and stored procedures where we could. 对于这个特定项目,我们尽可能使用现有的数据库和存储过程。 I have one stored procedure that is bringing in more columns of data than the model has. 我有一个存储过程所带来的数据列比模型多。 We had worked around using that stored procedure, but now the data that it is returning has been requested again. 我们已经解决了使用该存储过程的问题,但是现在已经再次请求了它返回的数据。 I am not sure of the best way to implement this, so I am turning here for help. 我不确定实现此目标的最佳方法,所以我在这里寻求帮助。

Here is my stored procedure: 这是我的存储过程:

CREATE Procedure dbo.procGetMainInvoices
  @Division CHAR(5),
  @CustNum  CHAR(6)
AS
SELECT  i.InvoiceNumber,
        i.Division,
        i.CustomerNumber,
        i.InvoiceType,
        i.InvoiceDate,
        i.DueDate,
        i.AmountDue,
        (SELECT COUNT(trn.InvoiceNumber)
         FROM   Invoice trn
         WHERE  trn.InvoiceNumber = i.InvoiceNumber
         AND    trn.Division = i.Division
         AND    trn.CustomerNumber = i.CustomerNumber) as TransactionCount
FROM    Invoice i
WHERE   i.Division = @Division
AND     i.CustomerNumber = @CustNum
AND     i.InvoiceStatus = 'O'
AND     i.ItemSequence = 0
AND     i.ItemLine = 0
ORDER BY i.InvoiceDate,
         i.InvoiceNumber

As you can see, the last column being selected is a count. 如您所见,最后选择的一列是一个计数。 I need to get that data added to my existing invoice model. 我需要将这些数据添加到我现有的发票模型中。 I need it done efficiently because I could be reading hundreds or thousands of records at a time. 我需要高效地完成它,因为我一次可以读取数百或数千条记录。

I got this working by converting the stored procedure to a view. 我通过将存储过程转换为视图来完成这项工作。 Then binding my DBContext to the view. 然后将我的DBContext绑定到视图。 I had to create a new model that included the fields in the view, but it did give me the flexibility that I needed to be able to filter my results to same degree as the stored procedure. 我必须创建一个新模型,其中包括视图中的字段,但这确实为我提供了所需的灵活性,以便能够将我的结果过滤到与存储过程相同的程度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM