16

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.

Serexx
  • 1,232
  • 1
  • 15
  • 32
  • 1
    Which VS version exactly? – ErikEJ May 29 '23 at 05:16
  • thanks, yes looks like a duplicate posted around the same time as mine. I've solved my issue by firing up VS 2019 to do the .EDMX create, but I've noted the workaround now for future. – Serexx May 29 '23 at 23:17

3 Answers3

30

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

Chris Bednarski
  • 3,364
  • 25
  • 33
1

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

1

I did find the same thing as in the answer (thanks) 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.

Serexx
  • 1,232
  • 1
  • 15
  • 32