Skip to main content

Data import and export

The Batch project's import/export command lets you export data from a database to a file, then import it into another database.

This command uses the Model project to list the tables to export, as well as the configuration defined in appSettings.json. Make sure the Model project matches the database and that the connection string is correct.

Exporting data​

You can use the dump command followed by the -e, or --export, option, ending with the path of a folder or file to which the data will be exported.

monprojet dump -f -e C:\Temp\DumpBdd.zip

Data anonymization​

It is recommended to define data anonymization rules in the Model project when retrieving a dump from a client database, if only to comply with GDPR. To anonymize data when using the dump -e command, override the LoadAnonymizerRules method in your ApplicationDbContext.

Below is an example that anonymizes the data of the AspNetUsers table when exporting data:

ApplicationDbContext.cs
/// <summary>
/// Database context
/// </summary>
public class ApplicationDbContext : LgApplicationTrackedChangeDbContextGuid<ApplicationUser>
{
/// <summary>
/// Loads the set of rules used to anonymize data during data dump operations.
/// </summary>
/// <returns>The anonymization rules to apply to entities and their properties.</returns>
public override DbAnonymizerRules LoadAnonymizerRules()
{
// Les règles utilisent Bogus pour générer des valeurs réalistes sur les propriétés ciblées.
// La locale "fr" permet de produire des données au format français.
DbAnonymizerRules rules = new("fr");

// Cette règle remplace les informations nominatives des utilisateurs exportés.
rules.AddRule<ApplicationUser>((user, faker) =>
{
user.FirstName = faker.Name.FirstName();
user.LastName = faker.Name.LastName();
user.UserName = $"{user.FirstName}.{user.LastName}";
user.NormalizedUserName = user.UserName.ToUpper();
user.Email = $"{user.FirstName}.{user.LastName}@foo.invalid";
user.NormalizedEmail = user.Email.ToUpper();
});

// Retourne les règles configurées pour l'export.
return rules;
}
}

You can add new rules depending on the data handled by your application: subcontractor name, financial amount, or any other sensitive data.

Importing data​

You can use the dump command followed by the -i, or --import, option, ending with the path of the file to import.

monprojet dump -i C:\Temp\DumpBdd.zip
Caution

During import, the target database's data will be erased. Check the connection string in your appSettings.json before starting an import.

Summary of the dump command options​

OptionDescription
-e or --exportExport mode.
-i or --importImport mode.
-f or --forceIn export mode, overwrites the specified file if it already exists.
-na or --no-anonymizationDo not use the anonymization rules defined in the model.
File argumentName of the file containing the data to import, or to which data will be exported.

Checks before running​

StepWhy
Check the connection stringThe command uses the Batch project's configuration.
Check the modelThe Model project is used to list the tables to export.
Enable or disable anonymizationDumps from a client database must protect sensitive data.
Confirm the target database before importImport erases the target database's data.