Use Code First Migrations to Seed the Database

Download Completed Project

In this section, you will use Code First Migrations in EF to seed the database with test data.

From the Tools menu, select NuGet Package Manager, then select Package Manager Console. In the Package Manager Console window, enter the following command:


This command adds a folder named Migrations to your project, plus a code file named Configuration.cs in the Migrations folder.

Screenshot of the Solution Explorer showing the folder hierarchy with the Configuration dot c s file highlighted in blue.

Open the Configuration.cs file. Add the following using statement.

using BookService.Models;

Then add the following code to the Configuration.Seed method:

protected override void Seed(BookService.Models.BookServiceContext context)
    context.Authors.AddOrUpdate(x => x.Id,
        new Author() { Id = 1, Name = "Jane Austen" },
        new Author() { Id = 2, Name = "Charles Dickens" },
        new Author() { Id = 3, Name = "Miguel de Cervantes" }

    context.Books.AddOrUpdate(x => x.Id,
        new Book() { Id = 1, Title = "Pride and Prejudice", Year = 1813, AuthorId = 1, 
            Price = 9.99M, Genre = "Comedy of manners" },
        new Book() { Id = 2, Title = "Northanger Abbey", Year = 1817, AuthorId = 1, 
            Price = 12.95M, Genre = "Gothic parody" },
        new Book() { Id = 3, Title = "David Copperfield", Year = 1850, AuthorId = 2, 
            Price = 15, Genre = "Bildungsroman" },
        new Book() { Id = 4, Title = "Don Quixote", Year = 1617, AuthorId = 3, 
            Price = 8.95M, Genre = "Picaresque" }

In the Package Manager Console window, type the following commands:

Add-Migration Initial

The first command generates code that creates the database, and the second command executes that code. The database is created locally, using LocalDB.

Screenshot of the Package Manager Console window with the Enable Migrations, Add Migration Initial, and Update Database lines circled in red.

Explore the API (Optional)

Press F5 to run the application in debug mode. Visual Studio starts IIS Express and runs your web app. Visual Studio then launches a browser and opens the app's home page.

When Visual Studio runs a web project, it assigns a port number. In the image below, the port number is 50524. When you run the application, you'll see a different port number.

Screenshot of the application window launched from Visual Studio in debug mode with the A P I link circled in red and highlighted with a red arrow.

The home page is implemented using ASP.NET MVC. At the top of the page, there is a link that says "API". This link brings you to an auto-generated help page for the web API. (To learn how this help page is generated, and how you can add your own documentation to the page, see Creating Help Pages for ASP.NET Web API.) You can click on the help page links to see details about the API, including the request and response format.

Screenshot of the auto-generated help page showing a list of links to documentation for API features.

The API enables CRUD operations on the database. The following summarizes the API.

Authors Description
GET api/authors Get all authors.
GET api/authors/{id} Get an author by ID.
POST /api/authors Create a new author.
PUT /api/authors/{id} Update an existing author.
DELETE /api/authors/{id} Delete an author.
Books Description
GET /api/books Get all books.
GET /api/books/{id} Get a book by ID.
POST /api/books Create a new book.
PUT /api/books/{id} Update an existing book.
DELETE /api/books/{id} Delete a book.

View the Database (Optional)

When you ran the Update-Database command, EF created the database and called the Seed method. When you run the application locally, EF uses LocalDB. You can view the database in Visual Studio. From the View menu, select SQL Server Object Explorer.

Screenshot of the S Q L Server Object Explorer showing the S Q L Server item highlighted in blue and the Add S Q L Server item highlighted in yellow.

In the Connect to Server dialog, in the Server Name edit box, type "(localdb)\v11.0". Leave the Authentication option as "Windows Authentication". Click Connect.

Screenshot of the Connect to Server dialog showing the text local d b v 11 dot 0 in the Server name field and highlighted in blue.

Visual Studio connects to LocalDB and shows your existing databases in the SQL Server Object Explorer window. You can expand the nodes to see the tables that EF created.

Screenshot of the S Q L Server Object Explorer showing the folder hierarchy with the Book Service Context item highlighted in blue.

To view the data, right-click a table and select View Data.

Screenshot of the S Q L Server Object Explorer showing the d b o dot Books item highlighted in blue and the View Data item highlighted in yellow.

The following screenshot shows the results for the Books table. Notice that EF populated the database with the seed data, and the table contains the foreign key to the Authors table.

Screenshot of the Books table showing the database populated with seed data and the table containing the foreign key.