1
I know it’s something basic, but how do I make a relationship between Entities using the Entity Framework?
I have the Competition table and the Competition table. The Competition table must have an associated status (Normal, Cancelled and etc).
Below is the code of the classes:
[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 it is error occurs when I try to create a new Migration. Follow 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.
Like
Concursos
relates toStatusConcursos
?– novic
In the Contests table in the Statusid column you should have the Id for Statusconcursos.
– Daybson
The relationship is 1 for many where a Contests can have a Statusconcursos???
– novic