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

Asked

Viewed 171 times

0

Tested having problem with relation between Department and Employee tables

This returns 2 error: -"Entitytype 'Funcionario' has no key defined. Define the key for this Entitytype." -Employees' is based on type 'Employee' that has no Keys defined. And if I put [Key] in both continue the same thing, with msg "Failure in ALTER TABLE ALTER COLUMN because the 'Funcionnarioid' column does not exist in the 'Funcionario' table." She does have the Funcionnarioid field,

Department:

    public int DepartamentoID { get; set; }

    public string DEPARTAMENTO_NOME { get; set; }

    public string DEPARTAMENTO_SALA { get; set; }


    public string ANDAR { get; set; }

Functionary:

        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; }

1 answer

1


Check once again if the names are really hitting, if they are correct, do the following, on top of your Propriedade Primary Key Adds the 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; }

Functionary:

   [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 thus NOME_PROPRIEDADE because so is the nomenclature for Constantes And that might get in your way 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, there’s a very simple link explaining how it works and a few examples: http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.