I'm developing a WebAPI (.NET) application with EntityFramework Core. I have implemented the following entities:
Pais.cs
public class Pais
{
[Key]
public int Id { get; set; }
[MaxLength(100), Required]
public string Descricao { get; set; }
}
Status.cs
public class Estado
{
[Key]
public int Id { get; set; }
[Required]
public virtual Pais Pais { get; set; }
[Required]
public string Descricao { get; set; }
public string Sigla { get; set; }
}
City.cs
public class Cidade
{
[Key]
public int Id { get; set; }
public virtual Estado Estado { get; set; }
public string Descricao { get; set; }
}
When trying to insert a new record with JSON below:
{
"descricao": "Ribeirão Preto",
"estado": {
"id": 1,
"pais": {
"id": 1,
"descricao": "Brasil"
},
"descricao": "São Paulo",
"sigla": "SP"
}
}
I have the following return Cannot insert explicit value for identity column in table 'Pais' when IDENTITY_INSERT is set to OFF.
I think the system is trying to insert the country that is related to the city of JSON, but I do not understand why.