Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming conventions for Code First migrations

We are using code first migrations to keep our database and model in sync. At the moment we have the version number as name for the migration which clearly doesn't work out. The problem is that multiple migrations with the same name where created by different developers independent of each other for their local database. This led to some weird behavior as the IMigrationMetadata.Id was different because of the time stamp but the classes are partial with the same name.

What is the way to go to call these migrations? The examples are always ridiculously oversimplified: e.g. adding a property Readers result in migration AddReaders.

Or should the migrations be broken down to these little changes? Instead of having accumulate all the changes into one big migration. What if there are dependencies?

like image 812
TS. Avatar asked Nov 03 '14 10:11

TS.


People also ask

What are the types of code first migration available in Entity Framework?

Automated Migration. Automated Migration was first introduced in Entity framework 4.3. In automated migration you don't need to process database migration manually in the code file. For example, for each change you will also need to change in your domain classes.

Which command is used to start the migration commands in code first approach?

Now it's time to use the Code First Migration approach. Open Package Manager Console. Run Enable-Migrations command in a Package Manager console. This command added two more classes to your project in the Migrations folder.

Which of the below are default code first convention to map or create a primary key column in the database?

By default, EF creates all the DB objects into the dbo schema. EF will create a DB table with the entity class name suffixed by 's' e.g. Student domain class (entity) would map to the Students table. EF will create a primary key column for the property named Id or <Entity Class Name> + "Id" (case insensitive).


1 Answers

Yes, I think the best way is to break down changes to small units, with descriptive names. As with git, where you should commit often, with migrations you should migrate often. Not necessarily property by property, but containing a logical unit of work.

Like if you need to add two tables for some feature, add those two tables in one migration. Avoid making big migrations where your work for days changing models before creating a migration. Time is essential with avoiding conflicts.

If there are dependencies, one migration should contain related changes, so if another developer applies the migration, the application still works.

When a developer makes a migration, it should be immediately committed and synced (shared with other devs, in case you are not using git).

When you work with small units of change, merging and resolving conflicts becomes much easier.

like image 161
Esa Komulainen Avatar answered Oct 25 '22 13:10

Esa Komulainen