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 und downward migrations. All changes that were done to your domain classes are automatically added.

How to execute a migration

Once a migration was 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 like so:

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 could 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 you add 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 out of one 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. But once your migration was excuted it won't be executed again. This also has to be managed via 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 was already added will be deleted and therefore lost.



http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application
http://msdn.microsoft.com/de-de/data/jj591621.aspx


Last edited Jun 5, 2014 at 8:32 AM by mgesing, version 10