EF Core update-database System.Globalization.CultureNotFoundException: – C#

by
Maya Patel
.net-generic-math .net-8.0 c# entity-framework-core

Quick Fix: Set the ‘System.Globalization.Invariant’ property to ‘false’ in the ‘runtimeOptions’ section of your project file to use the current culture instead of the invariant culture.

The Problem:

An error message "System.Globalization.CultureNotFoundException: Only the invariant culture is supported in globalization-invariant mode." is being encountered when running the "update-database" command in the package manager console for database creation using EF Core 7.0.11. This seems to be due to the globalization-invariant mode being enabled.

The Solutions:

Solution 1: Check runtime options for globalization

The error is caused by the globalization-invariant mode being enabled. In this mode, only the invariant culture is supported. To fix the issue, you need to disable the globalization-invariant mode by setting the “System.Globalization.Invariant” property to false in the project’s runtime options.

  1. Open the project file (.csproj) in a text editor.
  2. Find the runtimeOptions section.
  3. Set the System.Globalization.Invariant property to false.
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <RuntimeOptions>
      <ConfigProperties>
        <System.Globalization.Invariant>false</System.Globalization.Invariant>
      </ConfigProperties>
    </RuntimeOptions>
  </PropertyGroup>

</Project>
  1. Save the project file.

  2. Rebuild the project.

The error should now be gone.

Solution 2: Update InvariantGlobalization to false

You need to change `InvariantGlobalization` to `false` in your project file. This setting controls whether or not the globalization-invariant mode is enabled. In globalization-invariant mode, only the invariant culture is supported. This setting must be set to `false` in order for EF Core to create the database correctly.

&lt;PropertyGroup&gt;
  &lt;TargetFramework&gt;net8.0&lt;/TargetFramework&gt;
  &lt;Nullable&gt;enable&lt;/Nullable&gt;
  &lt;ImplicitUsings&gt;enable&lt;/ImplicitUsings&gt;
  &lt;InvariantGlobalization&gt;false&lt;/InvariantGlobalization&gt;
&lt;/PropertyGroup&gt;

Q&A

What exception occurs when using update-database in package manager console?

System.Globalization.CultureNotFoundException is thrown.

What setting to change to resolve the error?

Set InvariantGlobalization to false in project file.

Where to find InvariantGlobalization?

In PropertyGroup of the project file.

Video Explanation:

The following video, titled "EF Core 8: Only the invariant culture is supported in globalization ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

An unhandled exception has occurred while executing the request. System.Globalization.CultureNotFoundException: Only the invariant culture ...