Getting Started
1. Set up
Restore the dependencies:
dotnet restore
Add the required NuGet package:
dotnet add package EFCore.BulkExtensions
Make sure your PostgreSQL database has these tables:
CountriesStatesCities
Tip: Use EF Core migrations if needed.
If the tables are missing, the seeder stops and asks you to create them.
2. Check the data file
The tool expects a file named countries_states_cities.json in its working directory. The file is included in the repository. If it is missing, download it from the original source and rename it to countries_states_cities.json:
countries+states+cities.json in dr5hn/countries-states-cities-database.
3. Run the seeder
The connection string is read from the first command-line argument. If no argument is given, it is read from the Geo_Connection_String environment variable.
With a command-line argument
dotnet run -- "Host=localhost;Database=GeoDb;Username=postgres;Password=<password>"
With an environment variable
set Geo_Connection_String=Host=localhost;Database=GeoDb;Username=postgres;Password=<password>
dotnet run
Run the executable in PowerShell
CountrySeeder.exe "Username=;Password=;Server=;Port=5432;Database=yourdb" --force
Options
| Flag / environment variable | Description |
|---|---|
--force | Forces seeding even if the tables already contain data. |
FORCE_SEED=true | Same as --force, for CI or container use. |
Without --force, the seeder stops when the tables already contain data. When it seeds, it first truncates the Cities, States and Countries tables (restarting identities, with CASCADE).
Example output
Checking table existence...
Cleaning tables...
Seeding starting...
Reading json data file...
Seeding...
✔ Completed!
All seed operations has been completed!