18

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?

Slate
  • 221
  • 5
  • 14
urlreader
  • 6,319
  • 7
  • 57
  • 91
  • *I added the table to model.edmx* -- That means "update model from database"? – Gert Arnold May 28 '23 at 17:43
  • Yes, added in database, then update model in vs2022 – urlreader May 28 '23 at 18:11
  • I suspect it is related some installations. It is a new win11 machine, new install of .Net and vs2022. Right now, I simply manually create the new entity files, add to the generated folder. It works, but just curious why t4 template not work anymore. – urlreader May 28 '23 at 19:04
  • 1
    I see this: https://developercommunity.visualstudio.com/t/Create-or-recreate-a-Model-does-not-work/10318478?page=59&pageSize=15&sort=active&openOnly=false&closedOnly=false&topics=C++ , will try – urlreader May 28 '23 at 19:16
  • 1
    See also https://stackoverflow.com/q/76353796/861716 – Gert Arnold May 31 '23 at 11:26

2 Answers2

43

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.
Luca Ritossa
  • 1,118
  • 11
  • 22
  • 3
    Thank you! Just updated to VS 17.6 and thought I was losing my mind. – Neil Laslett May 30 '23 at 21:57
  • 1
    Thanks for spelling this out. The VB equivalent worked for me as well: Editing EF6.Utility.VB.ttinclude and replacing _generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance Or BindingFlags.NonPublic) with: _generationEnvironment = type.GetProperty("GenerationEnvironment") – Plate Jun 05 '23 at 01:17
  • @NeilLaslett So do I :( – stefmex Jun 07 '23 at 05:34
  • Thanks! I also had to edit the non-EF6 template, but then (after restarting VS) it worked So edit both EF6.Utility.CS.ttinclude and EF.Utility.CS.ttinclude – Ruben Jun 14 '23 at 11:45
  • Thanks! Not even ChatGPT could solve this one! You saved my bacon! – user3308241 Jun 23 '23 at 22:52
1

They fixed this in the 17.6.4 but they did a copy-paste error in the vb version of the templates. It now gives the invalid character error ';'. They pasted the c# version in the vb version, with the ';', just remove it and it works.

The affected files are:

EF.Utility.VB.ttinclude EF6.Utility.VB.ttinclude

What a mess.

Marco Ortali
  • 73
  • 1
  • 10