cgratelli
cgratelli

Reputation: 95

Fluent API in Entity Framework to Join 2 Tables

I am following this documentation to join 2 tables using Fluent API

These are my entities:

public class Personas
{
    [Key]
    public int idPersona { get; set; }
    public String nombrePersona { get; set; }
    public String apellidoPersona { get; set; }
    public String loginPersona { get; set; }
    public int idRol { get; set; }
    public Rol rolPersona { get; set; }
}

public class Rol
{
    [Key]
    public int idRol{ get; set; }
    public String nombreRol { get; set; }
    public ICollection<Personas> personas { get; set; }
}

And this my DBcontext file

 public class AppDBContext:DbContext
{
    public AppDBContext(DbContextOptions<AppDBContext> options):base(options) {}

    public DbSet<Personas> tPersonas { get; set; }
    public DbSet<Rol> tRol { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Rol>().HasMany(s => s.personas);
        base.OnModelCreating(modelBuilder);
    }
}

First problem: The code is not complete but the result looks like this:

enter image description here

The result should look like this

enter image description here

Second problem: Documentation recommends using "DbModelBuilder" rather than "ModelBuilder" class, but Visual Studio autocompletes the method "OnModelCreating". For some reason i cant use "System.Data.Entity" and i allowed to use only "Microsoft.EntityFrameworkCore".

Please help

EDIT 1: This is the controller code

// GET api/<controller>/5
    [HttpGet("{id}")]
    public HttpResponseMessage Get(int id)
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        Personas persona = dbContext.tPersonas.FirstOrDefault(p => p.idPersona == id);
        if (persona != null)
        {
            response.Content = new StringContent(JsonConvert.SerializeObject(persona));
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return response;
        }
        else
        {
            response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            response.Content = new StringContent("error message");
            return response;
        }
    }

Upvotes: 1

Views: 1148

Answers (1)

tbdrz
tbdrz

Reputation: 2190

First problem: you have to include the related data with .Include().

[HttpGet("{id}")]
public async Task<ActionResult<Personas>> Get(int id)
{
    var persona = await dbContext.tPersonas.Inculde(x => x.rolPersona).SingleAsync(x => x.idPersona == id));

    if (persona == null)
    {
        return NotFound();
    }

    return persona;
}

Second problem: use ModelBuilder as mentioned in the official docs: https://learn.microsoft.com/en-us/ef/core/modeling/ for EF Core

In case of EF 6 (DbModelBuilder), have you istalled the EntityFramework package via nuget? https://www.nuget.org/packages/EntityFramework/

Upvotes: 2

Related Questions