I'm starting in Asp Net Core and I have the following question:
I have a Curso
template and a Unidade
template, a course has multiple units, and that unit can belong to more than one course.
I did the individual Course and Unit model, but my problem is being in the process of creating the relationship model, I was thinking in some way that it would receive a list of courses and a unit, but without success
Below are code examples of my project, while running I'm encountering the error Identity_Insert
.
Modelo de curso
public class Curso
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
[Key]
public long curId { get; set; }
[Required]
[StringLength(80)]
public string curDescricao { get; set; }
[Required]
[StringLength(1)]
public string curStatus { get; set; }
[StringLength(20)]
public string curCodExterno { get; set; }
[StringLength(60)]
public string curObservacao { get; set; }
}
Modelo de unidade
public class Unidade
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
[Key]
public long uniId { get; set; }
[Required]
[StringLength(80)]
public string uniDescricao { get; set; }
[Required]
[StringLength(1)]
public string uniStatus { get; set; }
[StringLength(20)]
public string uniCodExterno { get; set; }
public byte[] uniImagem { get; set; }
}
Modelo de CursoUnidade
public class CursoUnidade
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
[Key]
public long cuuId { get; set; }
/*[Required]
public long cuuCurId { get; set; }
[ForeignKey("cuuCurId")]*/
public List<Curso> Curso { get; set; }
/*[Required]
public long cuuUniId { get; set; }
[ForeignKey("cuuUniId")] */
public Unidade Unidade { get; set; }
}
Serviço de unidade
public void AddTeste(CursoUnidade cursoUnidade)
{
_contexto.Add(cursoUnidade);
_contexto.SaveChanges();
}