加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在EF 5中导入标量返回值的函数

发布时间:2020-12-16 00:08:53 所属栏目:百科 来源:网络整理
导读:我正在使用EF 5和.NET 4.5,我已经为我的数据库创建了一个模型并将我的函数导入到模型中,我可以成功导入TVF和SP但是我无法导入具有标量返回值的函数. 是否可以与设计师或我手动编辑edmx文件? 解决方法 向下滚动到此页面上标量值函数部分: Database First De
我正在使用EF 5和.NET 4.5,我已经为我的数据库创建了一个模型并将我的函数导入到模型中,我可以成功导入TVF和SP但是我无法导入具有标量返回值的函数.
是否可以与设计师或我手动编辑edmx文件?

解决方法

向下滚动到此页面上标量值函数部分:

Database First Development with Entity Framework 5 – Importing Scalar Valued Functions

你可以遵循这个丑陋的解决方法,或者你可以按照我在这个答案的最底部给出的建议.

以下是该文章的摘录(关于解决方法):

“This method requires some minor changes of the .edmx file’s xml,
directly. To do so,right-click on the .edmx file and select ‘Open
With…’,‘XML (Text) Editor’. This is how the functions looks in the
.edmx file before changes:

<Function Name="CountActivities" ReturnType="int" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <Parameter Name="personId" Type="int" Mode="In" />
</Function>
<Function Name="CountHydrations" ReturnType="int" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <Parameter Name="personId" Type="int" Mode="In" />
</Function>
<Function Name="CountMeals" ReturnType="int" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <Parameter Name="personId" Type="int" Mode="In" />
</Function>

“Remove the ‘ReturnType’ attribute from the element. Then,
add a element to each of the elements.
See the modified .edmx file below for the contents of the elements.

<Function Name="CountActivities" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <CommandText>
        SELECT [dbo].[CountActivities] (@personId)
    </CommandText>
    <Parameter Name="personId" Type="int" Mode="In" />
</Function>
<Function Name="CountHydrations" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <CommandText>
        SELECT [dbo].[CountHydrations] (@personId)
    </CommandText>
    <Parameter Name="personId" Type="int" Mode="In" />
</Function>
<Function Name="CountMeals" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
    <CommandText>
        SELECT [dbo].[CountMeals] (@personId)
    </CommandText>
    <Parameter Name="personId" Type="int" Mode="In" />
</Function>

“Next,in the Model Browser,right-click on the ‘Function Imports’
folder and select ‘Add Function Import…’ This brings up the ‘Add
Function Import’ dialog window. We will import the ‘CountActivities’
scalar-valued function to show this method. Enter the following
information in the dialog window and select Save.”

我的建议:
考虑到所需的工作量,只需创建一个只返回一行以实现相同效果和目的的表值函数就更容易了.

以下是SQL Server中用户定义的表值函数的示例:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[udfGetTotalMinutesInvoiced] ( @billingType INT )
RETURNS TABLE
AS
RETURN
    (
      SELECT
            SUM([ProgressNote].[TotalMinutes]) AS 'TotalMinutesInvoiced'
        FROM
            [dbo].[InvoiceEntry]
        JOIN [dbo].[ProgressNote]
            ON [ProgressNote].[ProgressNoteID] = [InvoiceEntry].[ProgressNoteID]
        WHERE
            [InvoiceEntry].[BillingTypeID] = @billingType
            AND progressNote.[IsRecordedInInvoiceEntry] = 1
    )
GO

另请注意,您实际上可以在表值函数中调用标量值的用户定义函数.

要调用表值函数,您可能希望使用FirstOrDefault方法,如下所示:

private void UpdateStatisticsPanel()
        {
            var billingTypeId = int.Parse(txtBillingTypeId.Text);

            var totalMinutesInvoiced = context.udfGetTotalMinutesInvoiced(billingType: billingTypeId);
            var minutesInvoiced = totalMinutesInvoiced.FirstOrDefault();
            var invoiced = new Tuple<string,int?>("totalMinutesInvoiced:",minutesInvoiced);
            lstFinancialSummary.Items.Add(invoiced);

            var totalMinutesNotInvoiced = context.udfGetTotalMinutesNotInvoiced(billingType: billingTypeId);
            var minutesNotInvoiced = totalMinutesNotInvoiced.FirstOrDefault();
            var notInvoiced = new Tuple<string,int?>("totalMinutesNotInvoiced:",minutesNotInvoiced);

            lstFinancialSummary.Items.Add(notInvoiced);
            // remember to push the values up to the ListView.
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读