Bug in the ADO.NET Entity Framework POCO Generator?

Posted: August 3, 2010 in ADO.NET Entity Framework
Tags: , ,

Ok I give up. Is someone able to explain to me why the VB POCO generator T4 template refuses to generate my function imports. Not only that, but it doesn’t even generate the region for the function imports.
This pic shows the T4 code clearly defining the function import region

And this pic clearly shows the code NOT being generated.

Any ideas?

UPDATE: Found the problem

To answer my question, it seems that yes there is a bug. Looking at the template I cam across this nugget:

If edmFunction.ReturnParameter Is Nothing Then
  Continue For
End If

Essentially what this does is checks the return type of the import function and quits the rest of the iteration if the import function has no return type. Seems logical? NO.

You see in VB functions that don’t return a value are Subs. Since the generator can only handle functions, any function imports that don’t return a collection will not be generated. This is an issue for non-query functions.

You got a couple of options:

  1. Ensure your procs ALWAYS return something. Not always feasible.
  2. Modify the T4 template to handle Subs

I will try options 2.

Keep you posted

UPDATE: Found a potential solution

Ok I did my work around and replaced my “Function Imports” section in the T4 template with the following:

<#
region.Begin("Function Imports")
#>
<#
For Each edmFunction As EdmFunction In container.FunctionImports
Dim parameters As IEnumerable(Of FunctionImportParameter)  = FunctionImportParameter.Create(edmFunction.Parameters, code, ef)
Dim paramList As String = String.Join(", ", parameters.Select(Function(p) "ByVal " & p.FunctionParameterName & " As " & p.FunctionParameterType).ToArray())
If edmFunction.ReturnParameter Is Nothing Then
#>
<#=Accessibility.ForMethod(edmFunction)#> Sub <#=code.Escape(edmFunction)#>(<#=paramList #>)
<#
For Each parameter As FunctionImportParameter In parameters
If Not parameter.NeedsLocalVariable Then
Continue For
End If
#>
Dim <#=parameter.LocalVariableName #> As ObjectParameter
If <#=If(parameter.IsNullableOfT, parameter.FunctionParameterName & ".HasValue", parameter.FunctionParameterName & " IsNot Nothing")#> Then
<#=parameter.LocalVariableName#> = New ObjectParameter("<#=parameter.EsqlParameterName#>", <#=parameter.FunctionParameterName #>)
Else
<#=parameter.LocalVariableName#> = New ObjectParameter("<#=parameter.EsqlParameterName#>", GetType(<#=parameter.RawClrTypeName #>))
End If
<#
Next
#>
MyBase.ExecuteFunction("<#=edmFunction.Name#>"<#=code.StringBefore(", ", String.Join(", ", parameters.Select(Function(p) p.ExecuteParameterName).ToArray()))#>)
End Sub
<#
Else
Dim returnTypeElement As String = code.Escape(ef.GetElementType(edmFunction.ReturnParameter.TypeUsage))
#>
<#=Accessibility.ForMethod(edmFunction)#> Function <#=code.Escape(edmFunction)#>(<#=paramList #>) As ObjectResult(Of <#=returnTypeElement #>)
<#
For Each parameter As FunctionImportParameter In parameters
If Not parameter.NeedsLocalVariable Then
Continue For
End If
#>
Dim <#=parameter.LocalVariableName #> As ObjectParameter
If <#=If(parameter.IsNullableOfT, parameter.FunctionParameterName & ".HasValue", parameter.FunctionParameterName & " IsNot Nothing")#> Then
<#=parameter.LocalVariableName#> = New ObjectParameter("<#=parameter.EsqlParameterName#>", <#=parameter.FunctionParameterName #>)
Else
<#=parameter.LocalVariableName#> = New ObjectParameter("<#=parameter.EsqlParameterName#>", GetType(<#=parameter.RawClrTypeName #>))
End If
<#
Next
#>
Return MyBase.ExecuteFunction(Of <#=returnTypeElement#>)("<#=edmFunction.Name#>"<#=code.StringBefore(", ", String.Join(", ", parameters.Select(Function(p) p.ExecuteParameterName).ToArray()))#>)
End Function
<#
End If
Next
region.End()
#>

It seems to build and generate what seems to be a decent Sub. Please note, this is UNTESTED.

Advertisements
Comments
  1. […] version of the POCO class generator there is a bug in the function import code, I blogged on this here. You will have to code around this. To be fair, I haven’t tested the ObjectResult return […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s