There are multiple versions of this document. Pick the options that suit you best.

UI
Database

Web Application Development Tutorial - Part 7: Authors: Database Integration

About This Tutorial

In this tutorial series, you will build an ABP based web application named Acme.BookStore. This application is used to manage a list of books and their authors. It is developed using the following technologies:

  • Entity Framework Core as the ORM provider.
  • Angular as the UI Framework.

This tutorial is organized as the following parts;

Download the Source Code

This tutorial has multiple versions based on your UI and Database preferences. We've prepared a few combinations of the source code to be downloaded:

If you encounter the "filename too long" or "unzip error" on Windows, it's probably related to the Windows maximum file path limitation. Windows has a maximum file path limitation of 250 characters. To solve this, enable the long path option in Windows 10.

If you face long path errors related to Git, try the following command to enable long paths in Windows. See https://github.com/msysgit/msysgit/wiki/Git-cannot-create-a-file-or-directory-with-a-long-path git config --system core.longpaths true

Introduction

This part explains how to configure the database integration for the Author entity introduced in the previous part.

DB Context

Open the BookStoreDbContext in the Acme.BookStore.EntityFrameworkCore project and add the following DbSet property:

public DbSet<Author> Authors { get; set; }

Then locate to the OnModelCreating method in BookStoreDbContext class in the same project and add the following lines to the end of the method:

builder.Entity<Author>(b =>
{
    b.ToTable(BookStoreConsts.DbTablePrefix + "Authors",
        BookStoreConsts.DbSchema);
    
    b.ConfigureByConvention();
    
    b.Property(x => x.Name)
        .IsRequired()
        .HasMaxLength(AuthorConsts.MaxNameLength);

    b.HasIndex(x => x.Name);
});

This is just like done for the Book entity before, so no need to explain again.

Create a new Database Migration

The startup solution is configured to use Entity Framework Core Code First Migrations. Since we've changed the database mapping configuration, we should create a new migration and apply changes to the database.

Open a command-line terminal in the directory of the Acme.BookStore.EntityFrameworkCore project and type the following command:

dotnet ef migrations add Added_Authors

This will add a new migration class to the project:

bookstore-efcore-migration-authors

You can apply changes to the database using the following command, in the same command-line terminal:

dotnet ef database update

If you are using Visual Studio, you may want to use Add-Migration Added_Authors -c BookStoreDbContext and Update-Database -Context BookStoreDbContext commands in the Package Manager Console (PMC). In this case, ensure that Acme.BookStore.HttpApi.Host is the startup project and Acme.BookStore.EntityFrameworkCore is the Default Project in PMC.

Implementing the IAuthorRepository

Create a new class, named EfCoreAuthorRepository inside the Acme.BookStore.EntityFrameworkCore project (in the Authors folder) and paste the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Acme.BookStore.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Acme.BookStore.Authors
{
    public class EfCoreAuthorRepository
        : EfCoreRepository<BookStoreDbContext, Author, Guid>,
            IAuthorRepository
    {
        public EfCoreAuthorRepository(
            IDbContextProvider<BookStoreDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        public async Task<Author> FindByNameAsync(string name)
        {
            var dbSet = await GetDbSetAsync();
            return await dbSet.FirstOrDefaultAsync(author => author.Name == name);
        }

        public async Task<List<Author>> GetListAsync(
            int skipCount,
            int maxResultCount,
            string sorting,
            string filter = null)
        {
            var dbSet = await GetDbSetAsync();
            return await dbSet
                .WhereIf(
                    !filter.IsNullOrWhiteSpace(),
                    author => author.Name.Contains(filter)
                 )
                .OrderBy(sorting)
                .Skip(skipCount)
                .Take(maxResultCount)
                .ToListAsync();
        }
    }
}
  • Inherited from the EfCoreRepository, so it inherits the standard repository method implementations.
  • WhereIf is a shortcut extension method of the ABP Framework. It adds the Where condition only if the first condition meets (it filters by name, only if the filter was provided). You could do the same yourself, but these type of shortcut methods makes our life easier.
  • sorting can be a string like Name, Name ASC or Name DESC. It is possible by using the System.Linq.Dynamic.Core NuGet package.

See the EF Core Integration document for more information on the EF Core based repositories.

The Next Part

See the next part of this tutorial.

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Register Now
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More