简体   繁体   中英

Entity Framework Core 2.0 CRUD best practices / automation

Are there established best practices for a CRUD in EF Core 2.0?

Is there any way to automatically generate CRUDs for the models, preferably using these best practices?

As an example, this is what I have currently, but I have to replicate it to something like 20 other entities:

[Route("entities")]
public class EntitiesController : Controller
{
    private readonly ProjectContext _context;

    public EntitiesController(ProjectContext context)
    {
        _context = context;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody]Entities entity)
    {
        _context.Add(entity);
        return Json(await _context.SaveChangesAsync() >= 1);
    }

    [HttpGet]
    public JsonResult Get(int? entityId)
    {
        if (entityId == null)
            return Json(_context.Entities.Select(x => x));
        else
            return Json(_context.Entities.FirstOrDefault(x => x.EntityId == entityId));
    }

    [HttpPut]
    public async Task<IActionResult> Put([FromBody]Entities entity)
    {
        if (entity.EntityId < 1)
            return BadRequest();

        _context.Entities.Update(entity);
        return Json(await _context.SaveChangesAsync() >= 1);
    }

    [HttpDelete]
    public async Task<IActionResult> Delete(int entityId)
    {
        if (entityId < 1)
            return BadRequest();

        Entities entity = _context.Entities.Where(x => x.EntityId == entityId).FirstOrDefault();
        if (entity == null)
            return BadRequest();

        _context.Entities.Remove(entity);
        return Json(await _context.SaveChangesAsync() >= 1);
    }
}

There is an insightful article in Micorosoft's website . In this article, they have defined some of the best practices. For example there is a controversial debate about using DbContext directly or a repository and they have addressed this issue as follows:

The Entity Framework DbContext class is based on the Unit of Work and Repository patterns, and can be used directly from your code, such as from an ASP.NET Core MVC controller. That is the way you can create the simplest code, as in the CRUD catalog microservice in eShopOnContainers. In cases where you want the simplest code possible, you might want to directly use the DbContext class, as many developers do.

However, implementing custom repositories provides several benefits when implementing more complex microservices or applications. The Unit of Work and Repository patterns are intended to encapsulate the infrastructure persistence layer so it is decoupled from the application and domain model layers. Implementing these patterns can facilitate the use of mock repositories simulating access to the database.

So, as the first step maybe you should change the way you are using Dbcontext and use repositories instead of that. There are many more points and hints in this article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM