Saturday 31 October 2015

.NET Entity Framework: Using Database Migration to Seed a Database

The Entity Framework (EF) is an object-relational database mapper that enables easy database access for .NET developers using domain-specific .NET objects that EF can generate for you. As shown in the Using Database Migration post, you can easily use EF to create your database from the classes that are defined in your project code. You can then use Code First Migrations to handle any database changes for the life cycle of your application.

During the course of developing your application (or during the testing phase), you often make changes either to the data collected or to the way data is processed. Sometimes you may have a bug in your code that has let corrupt or incorrect data get into your database. This can give you unpredictable results, and you can end up dropping and creating a database multiple times. When this happens, you are sometimes faced with the undesirable task of getting data back into the database to test your bug fixes. In the past this was a painstakingly slow process, since you had to add all of your test data back into the database.
Well, Entity Framework and Code First Migration has a solution for that. In the last blog post on Data Migration, you saw that running theEnable-Migrations -ContextTypeName SafariCodeFirst.SeminarContext command created a Migrations folder and two C# source files:
As previously described, the Configuration.cs file contains the settings for Migrations to work its magic, which in this case is to seed the database. If you examine Configuration.cs, you will see that Migrations has already created a stub method called Seed as the code below shows. It even contains some commented out code that shows how to go about adding a seed data to the table.
Let’s remove the commented out code (or leave it for future reference) and add the code to seed the database as shown here:
You can see how the Context.Seminars.AddOrUpdate helper class is used to create three new seminars if they don’t exist. If the database rows exist, then they will be updated. One thing to note is that you need the line that reads p => p.Name, or you will add all three seminars each time you run Update-Database, since that is used to identify the records.
So now that the code is ready, let’s delete all of the rows in the table and then runUpdate-Database. Your database should look like the following figure after the command finishes running:
Let’s make one small change to the code and prove that the Seed method will update the existing rows if they already exist. Change the Speaker for “Seed Seminar Three” to read “Jim Smith” and run the Update-Database command again, and then check the results. You will see that this time, instead of adding records, the data was simply updated.
As shown here, you can use Entity Framework and Code First Migrations to seed your database and make your life as a developer a lot easier. Of course, there are other things that configuration.cs can be used for, but I hope I have piqued your interest in Entity Framework so you will start experimenting with it on your own.

No comments:

Post a Comment

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...