Problem has no key defined. Define the key for this EntityType

0

Tested having problem with relationship between department and employee tables

This returning 2 error: - "EntityType '' has no key defined. Define the key for this EntityType." -Official 'is based on type' Official 'that has no keys defined. And if I put [Key] in both continues the same thing, with msg "ALTER TABLE ALTER COLUMN failed because the 'FuncionnarioID' column does not exist in the 'Official' table." Since it does have the field FuncionarioID,

Department:

    public int DepartamentoID { get; set; }

    public string DEPARTAMENTO_NOME { get; set; }

    public string DEPARTAMENTO_SALA { get; set; }


    public string ANDAR { get; set; }

Officer:

        public int FuncionnarioID { get; set; }

    public string FUNCIONARIO_EMAIL { get; set; }
    public string FUNCIONARIO_NOME { get; set; }
    public string FUNCIONARIO_CIDADE { get; set; }

    public string FUNCIONARIO_ENDERECO { get; set; }


    public int DepartamentoID { get; set; }


    [ ForeignKey("DepartamentoID")]
    public virtual  Departamento Departamento { get; set; }
    
asked by anonymous 11.11.2017 / 15:29

1 answer

1

Check again if the names are actually beating, if they are correct, do the following, on top of your Propriedade Primary Key Add Data Annotation [Key] as follows:

Department:

[Key]
public int DepartamentoID { get; set; }

public string DEPARTAMENTO_NOME { get; set; }

public string DEPARTAMENTO_SALA { get; set; }

public string ANDAR { get; set; }

Officer:

   [Key]
   public int FuncionnarioID { get; set; }

   public string FUNCIONARIO_EMAIL { get; set; }

   public string FUNCIONARIO_NOME { get; set; }

   public string FUNCIONARIO_CIDADE { get; set; }

   public string FUNCIONARIO_ENDERECO { get; set; }

   public int DepartamentoID { get; set; }

   [ ForeignKey("DepartamentoID")]
   public virtual  Departamento Departamento { get; set; }

I advise you not to use the names of Propriedades like this NOME_PROPRIEDADE because this is the nomenclature for Constantes and this may distract you in the future, maybe you should use it this way:

Department:

[Key]
public int DepartamentoID { get; set; }

[Column("DEPARTAMENTO_NOME")]
public string  DepartamentoNome { get; set; }

[Column("DEPARTAMENTO_SALA")]
public string  DepartamentoSala { get; set; }

[Column("ANDAR")]
public string Andar { get; set; }

This will help you in the future, if you want to know a little more about it, it has a very simple link explaining how it works and some examples: link

    
20.02.2018 / 03:36