I have an old project which was built using VS2019 with Entity Framework 6 on .NET 4.5. We used database first to generate code. It worked very well.
However, we just started to use VS2022 V17.6.2, I need to update the application with a new table, so, I added the table to model.edmx, run the .tt file, to my surprise, it shows an error like this one:
Error Running transformation: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.VisualStudio.TextTemplatingD21DB4521EFD493FAE41A9CE9DA80C875F3084552987498BD518713BDE91D14AF116D5B591FBF7D992E37AB98D0755C27E1BD3D907AED6618E5F11743F78068A.GeneratedTextTransformation.DynamicTextTransformation.get_GenerationEnvironment() in C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude:line 1928 at Microsoft.VisualStudio.TextTemplatingD21DB4521EFD493FAE41A9CE9DA80C875F3084552987498BD518713BDE91D14AF116D5B591FBF7D992E37AB98D0755C27E1BD3D907AED6618E5F11743F78068A.GeneratedTextTransformation.EntityFrameworkTemplateFileManager..ctor(Object textTransformation) in C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude:line 1665 at Microsoft.VisualStudio.TextTemplatingD21DB4521EFD493FAE41A9CE9DA80C875F3084552987498BD518713BDE91D14AF116D5B591FBF7D992E37AB98D0755C27E1BD3D907AED6618E5F11743F78068A.GeneratedTextTransformation.EntityFrameworkTemplateFileManager.VsEntityFrameworkTemplateFileManager..ctor(Object textTemplating) in C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude:line 1784 at Microsoft.VisualStudio.TextTemplatingD21DB4521EFD493FAE41A9CE9DA80C875F3084552987498BD518713BDE91D14AF116D5B591FBF7D992E37AB98D0755C27E1BD3D907AED6618E5F11743F78068A.GeneratedTextTransformation.EntityFrameworkTemplateFileManager.Create(Object textTransformation) in C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude:line 1629 at Microsoft.VisualStudio.TextTemplatingD21DB4521EFD493FAE41A9CE9DA80C875F3084552987498BD518713BDE91D14AF116D5B591FBF7D992E37AB98D0755C27E1BD3D907AED6618E5F11743F78068A.GeneratedTextTransformation.TransformText() in C:\test\Code\ConsoleAppTest\TestModel.tt:line 10 Miscellaneous Files C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude 1928
So I compared the new .tt file with the old one, found there are some changes, for example, it was EF.Utility.CS.ttinclude not EF6.Utility.CS.ttinclude. So I tried the one we had before, still same error in VS2022.
I tried to apply the latest entity 6.4.4, changed to use .net framework 4.8, still the same. Tried to debug T4 template, still same error.

Now, I have to manually create the code for the newly added table. Is there a work around? Does this mean we can not use code generation in VS2022 for Entity Framework 6 for .NET Framework 4.8 (or below) anymore?
It's a known bug of Visual Studio 2022 v17.6+
As @urlreader wrote, there is currently an open issue to Microsoft Developer Community and a workaround is available HERE and HERE, waiting for a patch.
The workaround tells to:
C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes
private DynamicTextTransformation(object instance)
_generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);
_generationEnvironment = type.GetProperty("GenerationEnvironment");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With