EF Core Complex Object Insertion with Webapi

Asked

Viewed 184 times

0

I am developing a Webapi (.NET) application with Entityframework Core. I implemented the following entities:

Pais.Cs

public class Pais
{
    [Key]
    public int Id { get; set; }
    [MaxLength(100), Required]
    public string Descricao { get; set; }
}

Estado.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 enter 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.

It seems to me the system is trying to insert the Country that is related to the city of JSON, but I’m not sure why.

1 answer

2


Entityframework is trying to add a new Pais, and as it is not explicit that it is the EF that should create the Ids, the EF is waiting for the Country Id to be null or 0, thus passing the responsibility of the repository to create this Id.

However, this is not your belief. You just want to add a Cidade. A workaround is, before adding the new record Cidade in the RU, make a query for Estado and binds to the new object:

var estado = dbContext.Estados.Single(estado => estado.Id == novaCidade.Estado.Id);
novaCidade.Estado = estado;
dbContext.Cidades.Add(novaCidade);
dbContext.SaveChanges();

So your EF will know that the new Cidade has direct relation to an existing parent record in your repository.

But to really settle all this, review how you are setting up your EF. Consider maybe, or add a EstadoId in class Cidade, or create different repository models within the scope of the repository - which will require great effort in development, but the result is great.

  • So, but I’m not passing the city id in the json above. So wasn’t it supposed to generate automatically? And from what I understand of the error, he is trying to enter the country, and this already has the informed id.

  • Remembering I’m trying to add a new city.

  • I edited the answer, it should be clearer now.

  • Show, now it worked out. I’d just like to know one more thing. Do I really have to consult the Status object again? Because as you already pass the information on JSON you did not see the need to consult. Out that this may affect the performance of the application or not?

  • Yes you can, but that’s because of your architecture. If you create a property EstadoId in Cidade and let the EF know that this is a foreign key, you don’t need the first query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.