Serexx
Serexx

Reputation: 1250

Net Framework 4.8 EF6 EDMX T4 Error _generationEnvironment is NULL

This started with an EDMX suddenly not generating class files. The .context.cs file contains the class declaration like

  public virtual DbSet<myTable> myTable { get; set; }

but the actual class files are not being generated. Both .tt files are there & "Run Custom Tool" did not help.

On debugging the Model.tt T4 template this declaration:

public StringBuilder GenerationEnvironment { get { return (StringBuilder)_generationEnvironment.GetValue(_instance, null); } }

produces

"System.NullReferenceException: 'Object reference not set to an instance of an object.'"

the _generationEnvironment variable is NULL.

To be sure that database changes were not triggering this I recreated the Model including only a table that had not been modified, but with no improvement.

Unfortunately I don't have the luxury of moving this to Code-First and Core so any suggestions would be gratefully received.

Upvotes: 17

Views: 3154

Answers (3)

Serexx
Serexx

Reputation: 1250

I did find the same thing as in the answer in a round-about way - after repairing and then re-installing VS 2022 (pardon, could have mentioned the version) with no effect and being unable to find a way to roll back the Community version, I re-installed VS2019, created the .EDMX model there, no problems.

I'm continuing on in VS2022 now. Hopefully it gets fixed in the next release.

Upvotes: 1

Chris Bednarski
Chris Bednarski

Reputation: 3424

The Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation.GenerationEnvironment property access appears to have changed from non public to public in the latest update (ie. 17.6.2)

You can modify the EF.Utility.CS.ttinclude and EF6.Utility.CS.ttinclude files in C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes to fix this by changing

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

to

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

EDIT: Looks like it will be fixed in the next VS version https://github.com/dotnet/ef6tools/commit/89cd126fa8ebfd40c3b5e781232be940711cf726

Upvotes: 33

J. van Holten
J. van Holten

Reputation: 26

Same problem here. Using Visual Studio Community 2022 version 17.6.2

C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude:line 1928

Edit: Problem has gone after roll back to 17.5.3

Upvotes: 1

Related Questions