Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 6 t4 code generation not working in VS2022?

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.

A screenshot of Visual Studio. A line of code is raising an error. The code reads: public StringBuilder GenerationEnvironment { get { { return (StringBuilder)_generationEnvironment.GetValue(_instance, null); } }. The error that appears in the hovertext reads Exception Thrown. System.NullReferenceException: ‘Object reference not set to an instance of an object.’ _generationEnvironment was null.

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?

like image 252
urlreader Avatar asked Nov 20 '25 05:11

urlreader


1 Answers

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:

  1. Edit the file EF6.Utility.CS.ttinclude (you could need to edit EF.Utility.CS.ttinclude and, in case you develop in VisualBasic, please consider the '.VB.' version of these files) available here

C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes

  1. Search for the following string

private DynamicTextTransformation(object instance)

  1. Replace the setting of _generationEnvironment from

_generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);

  1. to this

_generationEnvironment = type.GetProperty("GenerationEnvironment");

  1. Now you can retry.
like image 51
Luca Ritossa Avatar answered Nov 21 '25 17:11

Luca Ritossa