I know it's something basic, but how do I make a relationship between Entities using the Entity Framework?
I have the Contest table and the StatusContacts. The Contest table must have an associated status (Normal, Canceled and etc.).
Below is the class code:
[Table("Concursos")]
public class Concurso
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Name = "Id")]
public int Id { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
[Display(Name = "Descrição")]
public string Descricao { get; set; }
[Required]
[DataType(DataType.DateTime, ErrorMessage = "A data de cadastro deve ser informada.")]
[Display(Name = "Data Cadastro")]
public DateTime DataCadastro { get; set; }
[Required]
[DataType(DataType.DateTime, ErrorMessage = "A data de início deve ser informada.")]
[Display(Name = "Data Início")]
public DateTime DataInicio { get; set; }
[DataType(DataType.DateTime)]
[Display(Name = "Data Fim")]
public DateTime DataFim { get; set; }
[Display(Name = "Observacao")]
[StringLength(500, ErrorMessage = "The {0} deve ter no maximo {1} letras.")]
public string Observacao { get; set; }
public int? StatusID { get; set; }
[ForeignKey("StatusID")]
public virtual StatusConcursos StatusConcurso { get; set; }
}
public class StatusConcursos
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Name = "Id")]
public int Id { get; set; }
[Required]
[Display(Name = "Descrição")]
public string Descricao { get; set; }
public virtual Concurso Concurso { get; set; }
}
The way this error occurs when trying to create a new migration. Here is the error message:
Unable to determine the principal end of an association between the types 'WellPlayed.Models.StatusConcursos' and 'WellPlayed.Models.Concurso'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.