Published on

ABP Getting Started With .Net Core and EF Core. ABP Part 1

3 min read
Table of Contents

Intro

In this post we will see how to create a CRUD API in ABP Application framework with dotnet core and ef core. It will be simple Todo api.

You can watch the videos or continue with the post.

IMAGE ALT TEXT HERE

Preparing the Project

Use the Getting started at https://docs.abp.io/en/abp/latest/getting-started?ui=mvc&db=ef&tiered=no guide to setup the basic abp application

1. Create an Entity

First step is to create an Entity. Create the Entity in the Domain project

public class Todo : Entity<Guid>
{
    public string Content { get; set; }
    public bool IsDone { get; set; }
}

2. Add Entity to ef core

Next is to add Entity to the EF Core. you will find the DbContext in the EntityFrameworkCore project. Add the DbSet to the DbContext

public DbSet<Todo> Todos { get; set; }

3. Configure Entity in ef core

Configuration is done in the DbContextModelCreatingExtensions class. This should be available in the EntityFrameworkCore project

builder.Entity<Todo>(b =>
{
    b.ToTable(TodosConsts.DbTablePrefix + "Todos", TodosConsts.DbSchema);
    b.ConfigureByConvention(); //auto configure for the base class props
});

4. Adding Migrations

Now the Entity is configured we can add the migrations.

Go the EntityFrameworkCore.DbMigrations project in the terminal and create migrations.

To create migration run this command:

dotnet ef migrations add created_todo

Verify the migrations created in the migrations folder.

To update the database run this command

dotnet ef database update

5. Create a Entity Dto

Dto are placed in Contracts project

public class TodoDto : EntityDto<Guid>
{
    public string Content { get; set; }
    public bool IsDone { get; set; }
}

6. Map Entity to Dto

Abp uses AutoMapper to map Entity to Dto. you can find the ApplicationAutoMapperProfile file which is used by the AutoMapper in the Application project.

CreateMap<Todo, TodoDto>();
CreateMap<TodoDto, Todo>();

7. Create an Application Services

Application service are created in the Application project

public class TodoAppService : YourProjectAppService
{
    private readonly IRepository<Todo, Guid> todoRepository;

    public TodoAppService(IRepository<Todo, Guid> todoRepository)
    {
        this.todoRepository = todoRepository;
    }

    public async Task<List<TodoDto>> GetAll()
    {
        return ObjectMapper.Map<List<Todo>, List<TodoDto>>(await todoRepository.GetListAsync());
    }

    public async Task<TodoDto> CreateAsync(TodoDto todoDto)
    {
        var todo = ObjectMapper.Map<TodoDto, Todo>(todoDto);
        var createdTodo = await todoRepository.InsertAsync(todo);
        return ObjectMapper.Map<Todo, TodoDto>(createdTodo);
    }

    public async Task<TodoDto> UpdateAsync(TodoDto todoDto)
    {
        var todo = ObjectMapper.Map<TodoDto, Todo>(todoDto);
        var createdTodo = await todoRepository.UpdateAsync(todo);
        return ObjectMapper.Map<Todo, TodoDto>(createdTodo);
    }

    public async Task<bool> DeleteAsync(Guid id)
    {
        var todo = await todoRepository.FirstOrDefaultAsync(x=> x.Id == id);
        if(todo != null)
        {
            await todoRepository.DeleteAsync(todo);
            return true;
        }
        return false;
    }
}

Once you created the Application configuration ABP can automatically configure your service as API controllers by conventions.