You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS22中EF5.0数据库优先模式更新模型遇EntityCommandCompilationException

问题:VS2022中EF5.0数据库优先模式下无法更新特定EDMX模型

问题详情

  • 环境:Visual Studio 2022,Entity Framework 5.0(数据库优先)
  • 现象:解决方案内3个同版本EF模型,仅1个无法从数据库更新EDMX文件;该模型在其他电脑或本机VS2019中可正常更新
  • 已尝试的无效操作:重新拉取源码、修复VS22、重启电脑/VS

异常信息

System.Data.Entity.Core.EntityCommandCompilationException: 准备命令定义时出错。有关详细信息,请参阅内部异常。 ---> System.NotImplementedException: 未实现该方法或操作。
   在 Microsoft.Data.Entity.Design.VersioningFacade.LegacyProviderWrapper.LegacyDbExpressionConverter.Visit(DbApplyExpression expression)
   在 Microsoft.Data.Entity.Design.VersioningFacade.LegacyProviderWrapper.LegacyDbExpressionConverter.Visit(DbProjectExpression expression)
   在 Microsoft.Data.Entity.Design.VersioningFacade.LegacyProviderWrapper.LegacyDbExpressionConverter.Visit(DbFilterExpression expression)
   在 Microsoft.Data.Entity.Design.VersioningFacade.LegacyProviderWrapper.LegacyDbExpressionConverter.Visit(DbProjectExpression expression)
   在 Microsoft.Data.Entity.Design.VersioningFacade.LegacyProviderWrapper.LegacyDbProviderServicesWrapper.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   在 System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory)
   --- 内部异常堆栈跟踪结束 ---
   在 System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory)
   在 System.Data.Entity.Core.EntityClient.EntityCommand.CreateCommandDefinition()
   在 System.Data.Entity.Core.EntityClient.EntityCommand.TryGetEntityCommandDefinitionFromQueryCache(EntityCommandDefinition& entityCommandDefinition)
   在 System.Data.Entity.Core.EntityClient.EntityCommand.GetCommandDefinition()
   在 System.Data.Entity.Core.EntityClient.EntityCommand.InnerPrepare()
   在 System.Data.Entity.Core.EntityClient.EntityCommand.ExecuteReader(CommandBehavior behavior)
   在 Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.EntityStoreSchemaGeneratorDatabaseSchemaLoader.LoadDataTable[T](String sql, Func`2 orderByFunc, DataTable table, EntityStoreSchemaFilterObjectTypes queryTypes, IEnumerable`1 filters, String[] filterAliases)
   在 Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.EntityStoreSchemaGeneratorDatabaseSchemaLoader.LoadRelationships(IEnumerable`1 filters)
   在 Microsoft.Data.Entity.Design.VersioningFacade.ReverseEngineerDb.SchemaDiscovery.EntityStoreSchemaGeneratorDatabaseSchemaLoader.LoadStoreSchemaDetails(IList`1 filters)
   在 Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.GetStoreSchemaDetails(StoreSchemaConnectionFactory connectionFactory)
   在 Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.CreateStoreModel()
   在 Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelGenerator.GenerateModel(List`1 errors)
   在 Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Engine.ModelBuilderEngine.GenerateModel(ModelBuilderSettings settings, IVsUtils vsUtils, ModelBuilderEngineHostContext hostContext)
   在 Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Gui.WizardPageBase.GenerateModel(ModelBuilderSettings settings)
   在 Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Gui.WizardPageUpdateFromDatabase.OnWizardFinish()
   在 Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.Gui.ModelBuilderWizardForm.OnFinish()
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   在 System.Windows.Forms.Button.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

解决方案

  • 清除VS组件缓存
    关闭VS2022,删除以下目录的缓存文件:

    • %LOCALAPPDATA%\Microsoft\VisualStudio\17.0_*\ComponentModelCache
    • %LOCALAPPDATA%\Temp\VSDebugger
      重启VS后尝试更新模型
  • 重置VS用户设置
    打开VS2022,依次点击工具 > 导入和导出设置 > 重置所有设置,选择重置为默认设置,完成后重启VS

  • 检查EDMX项目配置

    1. 右键目标EDMX文件,选择属性,确认元数据项目处理设置为嵌入到输出程序集
    2. 打开App.config/Web.config,检查连接字符串的提供程序是否为System.Data.SqlClient(针对SQL Server),避免使用过时的提供程序
  • 统一项目EF引用版本
    卸载该项目,编辑.csproj文件,确保EF相关引用版本统一为5.0.x:

    <PackageReference Include="EntityFramework" Version="5.0.0" />
    

    保存后重新加载项目,尝试更新模型

  • 临时切换到VS2019完成更新
    利用该模型在VS2019中可正常更新的特性,先在VS2019中完成模型更新,再回到VS22继续开发

内容的提问来源于stack exchange,提问作者Radek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 18:05:21