Adding migrations to an existing database

Sometimes, we have a use case where we want to add Entity Framework migrations to an existing database so that we can move from one way to handle schema changes to the migrations API. Of course, since our database is already in production, we need to let migrations know that we are starting with a known state. This is quite easy to do with another parameter to the Add-Migration commandlet: –IgnoreChanges. When we issue this command, Entity Framework will create an empty migration. It will assume that your model defined by context and entities are compatible with our database. Once you update the database by running this migration, no schema changes will take place, but a new row will be added to the _MigrationHistory table for this initial migration. Once this is accomplished, we can safely switch to the Entity Framework migration API to maintain schema changes from that point on.

Tip

Some database systems do not support underscore as the first character for a table name. Entity Framework allows developers to customize this name.

Another use case we want to address is when we also want to create entities for this existing database, thus adding Entity Framework not only to an existing database, but also to an existing software. This task can be accomplished with Entity Framework Power Tools. This Visual Studio extension is available on Visual Studio gallery at https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/. Once we install this extension, we will see a new option available on the right-click menu, Reverse Engineer Code First, at the project level.

All developers need to do is point this to the database they want to support with Entity Framework, and this tool will scaffold entities, context, and configuration classes for all entity classes for all tables in the database. We can also use Entity Framework Tools, which can be downloaded from the download center at http://www.microsoft.com/en-us/download/details.aspx?id=40762. This set of tools supports Code-First generation from a database as well. In order to use this functionality, we just need to select ADO.NET Entity Data Model from the Add New Item dialog and then follow the steps shown by the wizard.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.227.82