Net Framework 4.8 EF6 EDMX T4 错误 _generationEnvironment 为 NULL

huangapple go评论107阅读模式
英文:

Net Framework 4.8 EF6 EDMX T4 Error _generationEnvironment is NULL

问题

这是从一个EDMX突然不生成类文件开始的。.context.cs文件包含类声明,如下:

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

但实际的类文件没有生成。两个.tt文件都在那里,运行“Run Custom Tool”也没有帮助。

在调试Model.tt T4模板时,这个声明:

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

产生了以下错误:

"System.NullReferenceException: '对象引用未设置为对象的实例。'"

_generationEnvironment变量为空。

为了确保数据库更改不触发此问题,我重新创建了模型,只包含一个未被修改的表,但没有改善。

不幸的是,我没有将其迁移到Code-First和Core的余地,所以如果有任何建议,将不胜感激。

英文:

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

  public virtual DbSet&lt;myTable&gt; 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.

答案1

得分: 31

Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation.GenerationEnvironment 属性的访问权限似乎在最新更新中从非公开变为公开(即版本17.6.2)。

您可以通过修改 C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes 中的 EF.Utility.CS.ttincludeEF6.Utility.CS.ttinclude 文件来修复此问题,将以下代码:

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

更改为:

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

编辑:
看起来将在下一个 Visual Studio 版本中修复
https://github.com/dotnet/ef6tools/commit/89cd126fa8ebfd40c3b5e781232be940711cf726

英文:

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(&quot;GenerationEnvironment&quot;, BindingFlags.Instance | BindingFlags.NonPublic);

to

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

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

答案2

得分: 1

同样的问题。使用Visual Studio Community 2022版本17.6.2

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

编辑:回滚到17.5.3后问题已解决。

英文:

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

答案3

得分: 1

我确实找到了与答案中相同的东西(谢谢),但是是通过绕远路找到的 - 在修复然后重新安装了VS 2022(抱歉,可能应该提到版本)但没有效果,并且找不到回滚Community版本的方法后,我重新安装了VS2019,在那里创建了.EDMX模型,没有问题。

我现在继续在VS2022中工作。希望在下一个版本中能够修复这个问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月29日 06:15:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定