微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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.
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐