- Published on
ABP Getting Started With .Net Core and EF Core. ABP Part 1
Tags:
Series: ABP Framework - Getting started
This post is part of the ABP Framework - Getting started series.
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.
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.
Github Repo Link : https://github.com/antosubash/Todos
Related Posts
Continue reading with these related articles
Changing theme for your ABP app. Part 10
In this post we will explore how to change the theme for your ABP application.
Deploy ABP Framework dotnet core tiered app to docker swarm. Part 9
In this post we will see how to deploy your dotnet core app with docker container.
Centralized logging for .net core ABP microservices app using Seq. Part 8
In this post we will see how to implement a central logging system for your ABP app using Seq.
