执行Scaffold-DbContext命令遇全球化不变模式文化错误求助
问题场景
执行EF Core反向生成实体命令时触发区域性错误:
Scaffold-DbContext "connection string" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities
已尝试使用.NET 7、.NET 8预览版,且在项目配置中添加<InvariantGlobalization>true</InvariantGlobalization>,问题仍未解决。
项目配置
<PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <InvariantGlobalization>true</InvariantGlobalization> </PropertyGroup>
错误信息
System.Globalization.CultureNotFoundException: 在全球化不变模式下仅支持固定区域性。(参数“name”) en-us 是无效的区域性标识符。
在 System.Globalization.CultureInfo.GetCultureInfo(String name)
在 Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry, SqlConnectionOverrides overrides) 在 Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides) 在 Microsoft.Data.SqlClient.SqlConnection.Open() 在 Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(DbConnection connection, DatabaseModelFactoryOptions options) 在 Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(String connectionString, DatabaseModelFactoryOptions options) 在 Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions) 在 Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable1 schemas, IEnumerable1 tables, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable1 schemaFilters, IEnumerable1 tableFilters, String modelNamespace, String contextNamespace, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames, Boolean suppressOnConfiguring, Boolean noPluralize) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_0.<.ctor>b__0() 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.b__0()
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
在全球化不变模式下仅支持固定区域性。(参数“name”) en-us 是无效的区域性标识符。
解决方案
1. 移除全球化不变模式配置
直接从项目文件中删除<InvariantGlobalization>true</InvariantGlobalization>节点,让应用依赖系统默认的全球化区域性支持。
2. 临时禁用不变模式执行命令
如果需要保留项目的不变模式配置,可在执行命令前临时修改环境变量:
- Windows CMD:
set DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false Scaffold-DbContext "connection string" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities - PowerShell:
$env:DOTNET_SYSTEM_GLOBALIZATION_INVARIANT = "false" Scaffold-DbContext "connection string" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entities
3. 升级数据库驱动版本
将项目中的Microsoft.Data.SqlClient NuGet包升级至最新稳定版,新版本对全球化不变模式的兼容性有优化。
内容的提问来源于stack exchange,提问作者Zoinky

