是否可以与设计师或我手动编辑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.”
我的建议:
考虑到所需的工作量,只需创建一个只返回一行以实现相同效果和目的的表值函数就更容易了.
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. }