Problem with .Include () in the Entity Framework (Postgres)

7

I've been having a problem with EntityFramework, it's the following:

When I use the ORM (.Include) string to join tables to foreign keys, it automatically renames the keys in the string automatically generated by it and ends up not finding it in the database, following the models and string:

[Table("usuarios", Schema = "public")]
public partial class Usuario
{
    [Key]
    public Guid id { get; set; }
    public Guid nivel_id { get; set; }
    public Guid empresa_id { get; set; }
    public string nome { get; set; }
    public string email { get; set; }
    public string senha { get; set; }
    public DateTime datavencimento { get; set; }
    public bool ativo { get; set; }

    public virtual Nivel nivel { get; set; }
    public virtual Empresa empresa { get; set; }

And in the controller I use the following string:

public ActionResult Index()
    {
        var usuarios = db.Usuario
                        .Include(n => n.nivel)
                        .Include(e => e.empresa)
                        .ToList();

        return View(usuarios);
    }

And in debug mode, the EF select is like this, notice the end of the statement, it changes the name level_id by level_id1, because this is happening, is it some kind of "conflict" with the Postgres provider ?:

SELECT 1 AS "C1", "Extent1"."id", "Extent1"."nivel_id", "Extent1"."empresa_id", "Extent1"."nome", "Extent1"."email", "Extent1"."senha", "Extent1"."datavencimento", "Extent1"."ativo", "Extent2"."id" AS "id1", "Extent2"."nome" AS "nome1", "Extent3"."id" AS "id2", "Extent3"."cnpj", "Extent3"."razaosocial", "Extent3"."nomefantasia", "Extent3"."datafundacao", "Extent3"."contato", "Extent3"."cep", "Extent3"."ibge", "Extent3"."tipologradouro", "Extent3"."logradouro", "Extent3"."numero", "Extent3"."complemento", "Extent3"."bairro", "Extent3"."municipio", "Extent3"."estado", "Extent3"."site", "Extent3"."situacao" FROM "public"."usuarios" AS "Extent1" LEFT OUTER JOIN "public"."niveis" AS "Extent2" ON "**Extent1"."nivel_id1" = "Extent2"."id" LEFT OUTER JOIN "public"."empresas" AS "Extent3" ON "Extent1"."empresa_id1" = "Extent3"."id"

I would like to know why it does this and how to correct it without changing the existing database or template.

If anyone can help me, thank you in advance! Vlw

    
asked by anonymous 19.10.2015 / 17:11

1 answer

7

The elegant way to resolve this is by forcing the [ForeignKey] in Model :

[Table("usuarios", Schema = "public")]
public partial class Usuario
{
    [Key]
    public Guid id { get; set; }
    public Guid nivel_id { get; set; }
    public Guid empresa_id { get; set; }
    public string nome { get; set; }
    public string email { get; set; }
    public string senha { get; set; }
    public DateTime datavencimento { get; set; }
    public bool ativo { get; set; }

    [ForeignKey("nivel_id")]
    public virtual Nivel nivel { get; set; }
    [ForeignKey("empresa_id")]
    public virtual Empresa empresa { get; set; }
}

This is because the name of your foreign keys is slightly out of the Entity Framework naming convention, which does not use the underline ).

    
19.10.2015 / 17:16