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.
        }

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...