Generally, the relationship is made through the information of Foreign Keys (as Foreign Keys).
For annotations with attributes, it would look like this in Equipamento
:
public class Equipamento
{
[Key]
public int EquipamentoID { get; set; }
public string Nome { get; set; }
public string Sigla { get; set; }
// Conforme comentário do AP, essa propriedade não pode existir, ou o EF se perde.
//public virtual Databook databook { get; set; }
}
Note the use of virtual
. It is recommended especially when using the Entity Framework with proxies class usage setting. Here, the type property Databook
becomes only a navigation property.
A browsing property only signals to the Entitu Framework that a related record can be found in the specified entity. In this case, we are indicating that a Equipamento
may (or may not) have a Databook
.
And so in Databook
:
public class Databook
{
[Key]
public int DatabookID { get; set; }
[ForeignKey("equipamento")]
public int EquipamentoID { get; set; }
public virtual Equipamento equipamento { get; set; }
// ou assim:
// sendo esse caso o necessário para quando se trata de entidades
// com chaves compostas.
//
//[Key]
//public int EquipamentoID { get; set; }
//[ForeignKey("EequipamentoId")]
//public virtual Equipamento equipamento { get; set; }
public string Indentificacao { get; set; }
}
As already stated in @Tiagosilva’s reply, this approach works, but hurts the normalization and conventions of the Entity Framework.
– Leonel Sanches da Silva