起因:
之前版本vs2022生成ef模型一直没有问题,在更新了最新的vs2022之后,版本号17.6+,出现此问题:
正在运行转换:system.nullreferenceexception:未将对象引用设置到对象的实例。
具体错误如下:
正在运行转换: system.nullreferenceexception: 未将对象引用设置到对象的实例。
在 microsoft.visualstudio.texttemplating548217bd8377e326aee514eae722c9ba6586c4151f980ca7bacc70a5732fc55f588fbc8794326cd64388f9b4c50ad7729497de610ebc8d6f64f48bb130f68063.generatedtexttransformation.dynamictexttransformation.get_generationenvironment()
在 microsoft.visualstudio.texttemplating548217bd8377e326aee514eae722c9ba6586c4151f980ca7bacc70a5732fc55f588fbc8794326cd64388f9b4c50ad7729497de610ebc8d6f64f48bb130f68063.generatedtexttransformation.entityframeworktemplatefilemanager..ctor(object texttransformation)
在 microsoft.visualstudio.texttemplating548217bd8377e326aee514eae722c9ba6586c4151f980ca7bacc70a5732fc55f588fbc8794326cd64388f9b4c50ad7729497de610ebc8d6f64f48bb130f68063.generatedtexttransformation.entityframeworktemplatefilemanager.vsentityframeworktemplatefilemanager..ctor(object texttemplating)
在 microsoft.visualstudio.texttemplating548217bd8377e326aee514eae722c9ba6586c4151f980ca7bacc70a5732fc55f588fbc8794326cd64388f9b4c50ad7729497de610ebc8d6f64f48bb130f68063.generatedtexttransformation.entityframeworktemplatefilemanager.create(object texttransformation)
在 microsoft.visualstudio.texttemplating548217bd8377e326aee514eae722c9ba6586c4151f980ca7bacc70a5732fc55f588fbc8794326cd64388f9b4c50ad7729497de610ebc8d6f64f48bb130f68063.generatedtexttransformation.transformtext() webapplication7 f:\workspaces\d3mvcproject\webapplication7\webapplication7\models\model1.tt 1
同样操作,以前是可行的,但是更新完之后就不行了。经排查,受到一位大佬的启发,此为vs2022工具问题,微软官方暂时没有给出解决方案。
如果想要避免此错误:
方案如下:
方案一:
不要更新最新版,回退之前版本
方案二:
修改配置文件:
具体文件在你的vs安装目录下,这是默认安装路径
c:\program files\microsoft visual studio\2022\community\common7\ide\extensions\microsoft\entity framework tools\templates\includes
在这个目录下有一个文件名为ef6.utility.cs.ttinclude的配置文件
需要找到以下内容进行修改:
大约在1920行左右,需要将1920行的内容修改为1922行的内容即可。
可以在文件中搜索dynamictexttransformation这个方法,然后找到_generationenvironment,修改里面的内容,
内容修改为_generationenvironment = type.getproperty("generationenvironment");
附上具体的方法代码:
private dynamictexttransformation(object instance) { _instance = instance; type type = _instance.gettype(); _write = type.getmethod("write", new type[] { typeof(string) }); _writeline = type.getmethod("writeline", new type[] { typeof(string) }); //_generationenvironment = type.getproperty("generationenvironment", bindingflags.instance | bindingflags.nonpublic); //此为原来的代码 //_generationenvironment = type.getproperty("generationenvironment",bindingflags.instance| system.out.println); //这一行是测试代码,经测试,这个方法也不行 _generationenvironment = type.getproperty("generationenvironment"); //这个是正确代码,使用这行代码,可行 _host = type.getproperty("host"); _errors = type.getproperty("errors"); }
修改之后保存,重启vs,重新创建项目即可解决问题。
发表评论