Introduction

A Visual Studio 2015 project which shows how to perform the create, read, update and delete operations in the ASP.NET Core application using generic repository pattern with Entity Framework Core Code First approach.

The code illustrates the following topics:

  1. Listings, create, update and delete operations.
  2. One to many relationship in Entity Framework Core.
  3. Create, Update and Delete operations perform in bootstrap modal pop up with tag helpers.
  4. Database design using entity framework core code first approach with fluent API.
  5. Entity classes and repository in separate class library project and use migration to create database from it.
  6. Populate dropdown list in ASP.NET Core with dynamic data.
  7. Dependency injection for DbContext and repository.

Getting Started

To build and run this sample as-is, you must have Visual Studio 2015 installed. In most cases you can run the application by following these steps:

  1. Download and extract the .zip file.
  2. Open the solution file in Visual Studio.
  3. Change connection string in the appsettings.json file.
  4. Choose GR.Data project in package manager console and run the following command for migration and create database.
    • Tools –> NuGet Package Manager –> Package Manager Console
    • PM> Add-Migration MyFirstMigration
    • PM> Update-Database
  5. Run the application.

Running the Sample

To run the sample, hit F5 or choose the Debug | Start Debugging menu command. You will see the author list screen. From this screen you have author listing screen as shown in below figure.

Figure 1: Author listing

Now click on “Add Author” button to add new author in the application as per following screen.

Figure2: Add Author

As per figure 1, Edit button uses to edit individual author as per following figure.

Figure 3: Edit Author

As per figure 1, Book button uses to add new with respect an author as per following figure.

Figure 4: Add New Book

Now click on Book menu from the top and shows book listing screen as per following screen.

Figure 5: Book Listing

As per figure 5, Edit button uses to edit individual book as per following figure.

Figure 6: Edit Book

As per figure 5, Delete button uses to delete individual book as per following figure.

Figure 7: Delete Book

Source Code Overview

Most of folders play same role as in MVC application but there are following more folder and files.

  1. wwwroot: It holds static js and css files.
  2. GR.Data: It’s separate class library project. It holds DbContext, Entities and repository to perform database operations.
  3. project.json: It holds database connection string.
  4. Migrations: It holds database migration files.