Entity Framework 6在VS2022中的t4代码生成不起作用?

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

Entity Framework 6 t4 code generation not working in VS2022?

问题

我有一个旧项目,是在VS2019上使用Entity Framework 6和.NET 4.5构建的。我们使用数据库优先生成代码。它运行得很好。

然而,我们刚刚开始使用VS2022 V17.6.2,我需要用一个新表更新应用程序,所以我将表添加到model.edmx,运行.tt文件,令我惊讶的是,它显示了一个错误,类似于这样一个错误:

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 Studio22\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 Studio22\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 Studio22\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 Studio22\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 Studio22\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude	1928

所以我比较了新的.tt文件和旧的文件,发现有一些变化,例如,它是EF.Utility.CS.ttinclude而不是EF6.Utility.CS.ttinclude。所以我尝试使用我们之前使用的那个,但在VS2022中仍然出现相同的错误。

我尝试应用最新的Entity 6.4.4,将.NET Framework更改为4.8,仍然出现相同的错误。尝试调试T4模板,仍然出现相同的错误。

现在,我必须手动创建新添加的表的代码。是否有解决方法?这是否意味着我们不能再在VS2022中为.NET Framework 4.8(或更低版本)的Entity Framework 6使用代码生成了?

英文:

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:
> ```none
> 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?

答案1

得分: 43

这是Visual Studio 2022 v17.6+的已知问题。

正如@urlreader所提到的,目前在Microsoft开发者社区有一个已知问题,并提供了一个临时解决方案,等待修复。

解决方案如下:

  1. 编辑文件EF6.Utility.CS.ttinclude(如果你使用VisualBasic开发,请考虑编辑这些文件的**'.VB.'**版本),该文件位于以下位置:
C:\Program Files\Microsoft Visual Studio22\Professional\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes
  1. 搜索以下字符串:
private DynamicTextTransformation(object instance)
  1. 将**_generationEnvironment**的设置从以下内容:
_generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);

替换为:

_generationEnvironment = type.GetProperty("GenerationEnvironment");
  1. 现在可以重试了。
英文:

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
    <!-- language:c-sharp -->

<!-- language:c-sharp -->

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

  1. to this
    > _generationEnvironment = type.GetProperty("GenerationEnvironment");

  2. Now you can retry.

答案2

得分: 2

他们在17.6.4版本中修复了这个问题,但在vb版本的模板中出现了复制粘贴错误。现在会出现无效字符错误 '';''。
他们将c#版本粘贴到了vb版本中,包括 '';'',只需删除它,就可以正常工作。

受影响的文件是:

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

真是一团糟。

英文:

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.

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

发表评论

匿名网友

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

确定