Creating Migrations
Writing Migration Code
A migration is implemented as a class which derives from Migration. The actions to take are specified in the class’ overrides of the Migration.Up and Migration.Down methods.
The Migration class provides a number of methods that you can call to specify the actions to take in the migration. This section lists a few of the key methods; see the Migration class documentation for specific overloads, more details and other methods.
Method | Action | Typical SQL Equivalent |
AddTable | Adds a new table to the database | CREATE TABLE |
DropTable | Drops a table from the database | DROP TABLE |
RenameTable | Changes the name of an existing table | (varies) |
AddColumn | Adds a column to a table | ALTER TABLE ADD |
AddForeignKeyColumn | Adds a column with a foreign key constraint to a table | ALTER TABLE ADD |
DropColumn | Drops a column from a table | ALTER TABLE DROP |
ChangeColumn | Change the data type or nullability of a column | ALTER TABLE ALTER |
RenameColumn | Changes the name of a column | (varies) |
ExecuteNativeCommand | Executes arbitrary SQL. You can use this to make changes that are not supported through the migrations API, but it may make your migration code non‑portable across database providers. | (as specified) |
(SQL equivalents are approximate; the exact SQL syntax varies from database to database.)
Creating Migrations from a Model
The LightSpeed designer can create migrations for you. Migrations are created as source code which you can edit to reflect additional actions, conditional logic and overridden behaviour. Your model will give rise to a sequence of migrations as it evolves over time. Migrations are collected together in a migrations project separate from your application and model code.
Because migrations are created as C# or Visual Basic source code, they are stored in a Visual Studio project. You need to specify on your model which project it should store its migrations in.
To do this:
· Open your model, select the Migrations menu and choose Set Migrations Project.
· LightSpeed lists the existing projects in your solution.
· Choose a project and click OK, or click the New Project button.
· If you create a new project, LightSpeed prompts for the project file. Create a new folder and enter the desired project file name.
Don’t use the same migrations project for multiple models.
To create a migration in the migration project, select the Migrations menu and choose Create Migration.
· LightSpeed displays the actions required in the Up method to migrate the database from the last migration to the current model design, and the actions required in the Down method to reverse the process. You can de-select any actions that you don’t want generated for you.
· Enter a migration name and optionally a description. The migration name will be the name of the generated class and must be a valid class name in the migration project language.
· Click OK. LightSpeed generates and displays the migration class. You can now edit this class if required.
LightSpeed stores snapshots of your model in the migrations project, and uses these to work out what has changed. Do not move, delete or rename these snapshots!
Create Migrations from Scratch
Introduced in LightSpeed 5, empty migrations can be created for use with custom migration code.
Data Types
You will normally specify data types using the ModelDataType class. This abstracts your code from the database representation. For example, you would write ModelDataType.String rather than VARCHAR, NVARCHAR or VARCHAR2.
When you require fine control over data types, you can instead specify a literal string. For example, if you want a string to be represented as a fixed-size field, you could specify “CHAR” instead of ModelDataType.String. Such data types are database specific.
Identity Generation
The Migration class also provides APIs to create the database resources used by the LightSpeed identity generation methods. See the AddKeyTable API.
Initial Value When Adding a Table
Introduced in LightSpeed 5, the AddTable method now includes an overload to create a key table with an initial value. This will be used by default when generating migrations from the designer.
Migrating Entity Framework EDMX Model Files
LightSpeed now includes support for migrating EDMX V2 files, from Entity Framework, to the LightSpeed Model format. This allows you to import domain models created in the Entity Designer into LightSpeed.
Injecting Custom User Code
In LightSpeed 5, Before and After hooks after available, allowing custom user code to be injected into the workflow when deriving.