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.
  • MVC / Razor Pages as the UI Framework.

This tutorial is organized as the following parts;

Download the Source Code

This tutorials has multiple versions based on your UI and Database preferences. We’ve prepared two combinations of the source code to be downloaded:

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:

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

Then open the BookStoreDbContextModelCreatingExtensions class in the same project and add the following lines to the end of the ConfigureBookStore method:

  1. builder.Entity<Author>(b =>
  2. {
  3. b.ToTable(BookStoreConsts.DbTablePrefix + "Authors",
  4. BookStoreConsts.DbSchema);
  5. b.ConfigureByConvention();
  6. b.Property(x => x.Name)
  7. .IsRequired()
  8. .HasMaxLength(AuthorConsts.MaxNameLength);
  9. b.HasIndex(x => x.Name);
  10. });

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

Create a new Database Migration

Open the Package Manager Console on Visual Studio and ensure that the Default project is Acme.BookStore.EntityFrameworkCore.DbMigrations in the Package Manager Console, as shown on the picture below. Also, set the Acme.BookStore.Web as the startup project (right click it on the solution explorer and click to “Set as Startup Project”).

Run the following command to create a new database migration:

bookstore-add-migration-authors

This will create a new migration class. Then run the Update-Database command to create the table on the database.

See the Microsoft’s documentation for more about the EF Core database migrations.

Implementing the IAuthorRepository

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Dynamic.Core;
  5. using System.Threading.Tasks;
  6. using Acme.BookStore.EntityFrameworkCore;
  7. using Microsoft.EntityFrameworkCore;
  8. using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
  9. using Volo.Abp.EntityFrameworkCore;
  10. namespace Acme.BookStore.Authors
  11. {
  12. public class EfCoreAuthorRepository
  13. : EfCoreRepository<BookStoreDbContext, Author, Guid>,
  14. IAuthorRepository
  15. {
  16. public EfCoreAuthorRepository(
  17. IDbContextProvider<BookStoreDbContext> dbContextProvider)
  18. : base(dbContextProvider)
  19. {
  20. }
  21. public async Task<Author> FindByNameAsync(string name)
  22. {
  23. return await DbSet.FirstOrDefaultAsync(author => author.Name == name);
  24. }
  25. public async Task<List<Author>> GetListAsync(
  26. int skipCount,
  27. int maxResultCount,
  28. string sorting,
  29. string filter = null)
  30. {
  31. return await DbSet
  32. .WhereIf(
  33. !filter.IsNullOrWhiteSpace(),
  34. author => author.Name.Contains(filter)
  35. )
  36. .OrderBy(sorting)
  37. .Skip(skipCount)
  38. .Take(maxResultCount)
  39. .ToListAsync();
  40. }
  41. }
  42. }
  • Inherited from the EfCoreAuthorRepository, 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.