System.NullReferenceException with EF5 and .NET 4.5

Apr 1, 2013 at 9:16 AM
This is probably somewhat related to the Jan 10 discussion on Error Running transformation: System.NullReferenceException, (http://ef4templates.codeplex.com/discussions/429182).

It seems I was getting the same error, but I think I understand why. I was looking for differences with my (newly created) project and your sample downloaded from here.

I saw there was definitely a difference in folders/files. The "EntityGenCSharp" and EntityGenVB" folders aren't added using Nuget.... not sure if anything else... besides mine being CS.

Anyway, if I wanted to just take your sample project and remove things and make it my own, that might have worked, except I noticed that as soon as I switched from .NET 4 to .NET 4.5, things broke.

One thing had to do with a few remaining TransformationContext.Host needing to be TransformationContext.Current.Host, which I know you are aware of because I saw your comments on http://www.olegsych.com/2012/12/t4-toolbox-for-visual-studio-2012. At least in the code/version I had, I still found a few...

But even after replacing those, I continued to get the System.NullReferenceException. :(

I am super duper new to T4, in fact, this is my first venture into this..., or else I would persist down the rabbit hole to see what else I could find... but I think I will have to learn a bit more about this before I can be of help.

Also, fyi, in switching over to .NET 4.5, I made sure to uninstall and re-install EF 5, to be sure to use EF 5's .NET 4.5 binaries and not EF 5's .NET 4 binaries, as per Julie Lerman's post @ http://msdn.microsoft.com/en-us/magazine/jj618295.aspx

If you can provide any guidance, I would be most appreciative.
Thanks in advance!
Apr 1, 2013 at 9:58 AM
Sweet!!

So I was a little useful sooner than I thought.

It turns out that the problem had to do with the XML namespaces used in the EF4 vs EF5 edmx. Not sure if .NET 4 vs .NET 4.5 binaries for EF 5 matter.... probably not.

__Either way, here is the quick Hack solution:__

In the file EdmxParser.ttinclude, I made the following modification:
/// <summary>Entity Framework 4 constants.</summary>
internal static class EF4Constants
{
    // public static readonly XNamespace EdmxNamespace = "http://schemas.microsoft.com/ado/2008/10/edmx";
    // public static readonly XNamespace EfSmNamespace = "http://schemas.microsoft.com/ado/2009/02/edm/ssdl";
    // public static readonly XNamespace EfCmNamespace = "http://schemas.microsoft.com/ado/2008/09/edm";
    // public static readonly XNamespace EfMaNamespace = "http://schemas.microsoft.com/ado/2008/09/mapping/cs";

    public static readonly XNamespace EdmxNamespace = "http://schemas.microsoft.com/ado/2009/11/edmx";
    public static readonly XNamespace EfSmNamespace = "http://schemas.microsoft.com/ado/2009/11/edm/ssdl";
    public static readonly XNamespace EfCmNamespace = "http://schemas.microsoft.com/ado/2009/11/edm";
    public static readonly XNamespace EfMaNamespace = "http://schemas.microsoft.com/ado/2009/11/mapping/cs";
}
After that, all worked fine.

And for completeness, I'll report also that I returned to my newly created project that had nothing but the Nuget version of your rockin EF4T4, and once I made the above change, all worked there too! That project didn't have the "EntityGenCSharp" and EntityGenVB" folders I had metioned in my original post... still not sure what those are? I was able to generate using all options. Whatev.

Thanks agin for sharing your awesome project! :)