Documentation for SmartStore.NET 2.5
Entity Framework Migrations
Database updates in SmartStore.NET are done with the migration functionality of the Entity Framework
How to add a migration
Open the Nuget Package Manager Console:
Tools > Library Package Manager > Package Manager Console
Select SmartStore.Data as the default project and type
add-migration MyMigration
Now a c# class is automatically added to your solution within the SmartStore.Data project, which contains methods for upward and downward migrations. All changes that have been made to your domain classes are automatically added.
How to execute a migration
Once a migration has been added, it will be executed at the next start of the application. You can also run it via console by typing:
update-database
How to migrate locale resources
In order to migrate (add, update or delete) new locale string resources, you need to add the interface ILocaleResourcesProvider and IDataSeeder<SmartObjectContext> and implement their methods as follows:
public void Seed(SmartObjectContext context) { context.MigrateLocaleResources(MigrateLocaleResources); } public void MigrateLocaleResources(LocaleResourcesBuilder builder) { // delete resources builder.Delete("ResId1", "ResId2", "ResIdN"); // add or update resources builder.AddOrUpdate("MyResource.Id") .Value("My default resource value") .Value("de", "My resource value in German"); }
Â
How to migrate settings
In order to migrate setting entries, you also need to implement IDataSeeder<SmartObjectContext>. Then, you can add or delete your settings within the Seed method. You can even delete a whole group of settings.
public void Seed(SmartObjectContext context) { context.MigrateLocaleResources(MigrateLocaleResources); context.MigrateSettings(x => { x.Add("MySetting", 10); x.Delete("MyFirstSetting", "MySecondSetting"); x.DeleteGroup("ThemeSettings"); }); }
Â
How to add migrations to plugins
When you've changed the signature of one of your plugin specific domain entities, you first have to choose the build configuration EFMigrations before adding the migration to this project, otherwise Visual Studio won't find the correct path to the plugin assemblies.
You're able to migrate the plugin object context and the smartstore object context from a single migration. You just have to implement the IDataSeeder interface with both object contexts
public partial class MyMigration : DbMigration, IDataSeeder<MyPluginObjectContext>, IDataSeeder<SmartObjectContext>
public void Seed(MyPluginObjectContext context) { // seed your plugin context } public void Seed(SmartObjectContext context) { // seed the core context (e.g. locale resources, settings etc.) }
Â
How do I solve "No migrations configuration type was found in the assembly..."? Change the configuration to EFMigrations or set the nuget target manually. To do so, open the plugin project file with an editor and change the following line
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
Â
into
<Import Project="$(SolutionDir)\.nuget\nuget.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
Â
How to downgrade
Sometimes it is necessary to run a migration twice or more often. However, once your migration has been executed, it won't be executed again. This also has to be managed via the console:
update-database -TargetMigration MyMigration
Now your database will be downgraded to the specified migration. Keep in mind that all other migrations that come in order after the specified migration will be rolled back too and all data that has already been added will be deleted and therefore lost.