当前位置: 代码网 > it编程>编程语言>Asp.net > 关于VS2022使用EF生成实体模型报错的问题:运行转换:System.NullReferenceException:对象引用未设置为对象的示例。

关于VS2022使用EF生成实体模型报错的问题:运行转换:System.NullReferenceException:对象引用未设置为对象的示例。

2024年06月01日 Asp.net 我要评论
起因: 之前版本vs2022生成EF模型一直没有问题,在更新了最新的vs2022之后,版本号17.6+,出现此问题: 正在运行转换:System.NullReferenceException:未将对象引用设置到对象的实例。 具体错误如下: 正在运行转换: System.NullReferenceEx ...

起因:

之前版本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,重新创建项目即可解决问题。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com