How To : NopCommerce Best Practices for Database Updates using Entity Framework.
Compatible With: NopCommerce 3.7
These instructions will help you setup Entity Framework Migrations on the Nop.Data project with the assumption that you did a NopCommerce 3.7 fresh default database setup install with or without sample data. The NopCommerce default database install will be the base used.
Enabling Entity Framework Migrations will allow you to apply Entity Framework Code First to the database to update the default NopCommerce schema. The benefits are that you have a starting point database with NopCommerce and any changes that you apply after that are captured as individual database changes that are applied to the NopCommerce Default database. The Migrations can be reapplied later on to a Fresh NopCommerce install using the migrations.
Prerequisites:
- Fresh NopCommerce 3.7 Source Code Installed
- Fresh NopCommerce 3.7 Database Installed with or without sample data
Getting Started:
- Open NopCommerce Solution
- Change Presentations\Nop.Web\Web.Config to point to the database you want to use.
<connectionStrings>
<add name="NopCommerce" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=NopCommerce37;Integrated Security=False;User Id=sa;Password=yourpassword;MultipleActiveResultSets=True" />
</connectionStrings>
- Open Package Manager Console
- Set Default project: Nop.Data
- Type: Enable-Migrations - this will create a folder called Migrations under the Nop.Data project.
- Type: Add-Migration “InitialDatabaseSnapshot” –IgnoreChanges
Note: The –IgnoreChanges switch will tell our Nop.Data project to not compare the Nop.Data Migrations against the current database. Because we don't have any existing migrations, by not using the "-IgnoreChanges", it would create all the code to regenerate all the tables from NopCommerce duplicating what NopCommerce will create by default. Our Migrations are meant to capture changes moving forward after the default NopCommerce 3.7 Database is setup and be easily reapplied.
After you have ran this, the Nop.Data project will have a new folder called Migrations with two new files:
- Configuration.cs
- {DateTimeStamp}_InitialDatabaseSnapshot.cs – Initial place holder for Migrations. This has an empty Up() and Down() method intentionally because we used the "-IgnoreChanges" switch in our command earlier. It assumes that Created the tables and sample data and anything moving forward is newly added.
Next Steps: Applying the changes to the database
In Package Manager Console:
You should receive a confirmation that the project is ready.
This will enable Migrations in the database by adding a __Migrations table. The __Migrations table is a history of Migration changes applied to it. The newly added migration we created earlier InitialDatabaseSnapshot will to be applied to the default NopCommerce 3.7 database we setup earlier, but since we did the -IgnoreChanges switch, the {DateTimeStamp}_InitialDatabaseSnapshot.cs contains no code in Up() or Down(), so no changes are applied to the database with the exception of the newly added __Migrations table which is what we want. If you open the new __Migrations table, it will have a single record for the InitialDatabaseSnapshot migration that was applied to the database. Now that a record was captured for InitialDatabaseSnapshot, it will never be applied again to the database..
Your database and solution is now ready for Code First Updates using Migrations.
Next Steps:
ADDING A NEW ENTITY AND RUNNING ADD-MIGRATION
Important: These instructions will create Migrations code to generate tables after the adding of new Entities
Prerequisite:
- Added a newly Entity Person (Example: Person.cs) to the Nop.Core\Domain\Custom;
Steps:
- Open Presentations\Nop.Web\Web.Config and verify the connection string that it is pointing to the database you want to use.
- Open: Package Manager Console
- set Default Project: Libraries\Nop.Data
- Type: Add-Migration “SomeDescriptionHere”
- Example: Add-Migration “Add New Person Entity”
NOTE: After you run “Add-Migration”, the Nop.Data project will have a new file called
- {DateTimeStamp}_AddNewPersonEntity.cs
This new file contains code to generate any new tables from new Entities you have defined in Entity Framework in the Up () method and code to remove tables in the Down() method.
• Type: Update-Database
This will cause the newly added migration AddNewPersonEntity to be applied to the database and a new table called Person tables to be created. The AddNewPersonEntity migration will be tracked in the __Migrations table so it’s never applied again.