How to add items from a JSON to a database?

Asked

Viewed 37 times

0

Explaining: I have the following classes to work on, one that is the arrival and return of JSON and others, entities data, follows below:

User experiment result:

public class UsuarioExperimentoResult
{
    public string nome_usuario_experimento { get; set; }
    public string telefone_experimento { get; set; }
    public string email_experimento { get; set; }
    public string Id { get; internal set; }
    public List<EnderecoExperimento> endereco_expermento { get; set; }

}

Entity Data:

public class QuemAvisarExperimento : EntityData
{
    public string id_usuario_experimento_quem_avisar { get; set; }
    public string id_endereco_experimento_quem_avisar { get; set; }
    public string nome_experimento_quem_avisar { get; set; }
    public string telefone_experimento_quem_avisar { get; set; }
}

public class EnderecoExperimento : EntityData
{
    public string id_usuario_experimento { get; set; }
    public string nome_endereco_experimento { get; set; }
    public string destino_experimento { get; set; }
    public double lat_endereco_experimento { get; set; }
    public double lng_endereco_experimento { get; set; }
}

public class UsuarioExperimento : EntityData
{
    public string nome_usuario_experimento { get; set; }
    public string telefone_experimento { get; set; }
    public string email_experimento { get; set; }

}

So, I get the JSON and add the user, create an ID for it and done this, I should add the items that came in the list of type EnderecoExperimento. How can I do this, add item by item, in the database and related to the user ID previously created?

Here’s the method I created but it hasn’t worked:

private HttpResponseMessage operacaoPost(UsuarioExperimentoResult usuarioExperimentoResult)
{
    mContext context = new mContext();
    if (usuarioExperimentoResult != null)
    {
        // Adding User
        UsuarioExperimento usuario = new UsuarioExperimento
        {
            Id = Guid.NewGuid().ToString(), //id para novo cadastro
            nome_usuario_experimento = usuarioExperimentoResult.nome_usuario_experimento,
            telefone_experimento = usuarioExperimentoResult.telefone_experimento,
            email_experimento = usuarioExperimentoResult.email_experimento
        };

        context.UsuarioExperimentoDB.Add(usuario);

        // Adding Address
        if (usuarioExperimentoResult.endereco_expermento != null)
        {
            List<EnderecoExperimento> enderecoExperimentos = new List<EnderecoExperimento>();
            enderecoExperimentos = usuarioExperimentoResult.endereco_expermento;
            foreach (var end in usuarioExperimentoResult.endereco_expermento)
            {
                //gerar um id para cada endereço
                end.Id = Guid.NewGuid().ToString();

            }

        }
        // Saving the changes
        context.SaveChanges();
        return this.Request.CreateResponse(HttpStatusCode.OK, RequestResponseUtils.createResponseBody(usuario));
    }
    else
    {
        return this.Request.CreateResponse(HttpStatusCode.BadRequest, "404");
    }
}

Thank you!

Edit:

json:

{
    "dados":{"nome_usuario_experimento":"teste",
             "telefone_experimento":"0123456789",
             "email_experimento":"teste@teste",
             "endereco_expermento":[{"nome_endereco_experimento":"nome1",
                                      "destino_experimento":"destino1"
             }]
            }
}
  • it would be nice to show the json too, but normally vc deserializa on an object of type UsuarioExperimento in your case, and inside it the address list is already populated. when adding by Entity, it itself already inserts the addresses

  • The address has to link the user’s Id to it, so I saved the user before the address. And, I didn’t understand how to resolve. My foreach there, how to access the attributes of the Address Class to put in each item of the database? such as name, address and etc. User does not have foreign key, control is all done in code.

  • 2

    Entity does this... just put a user object inside the address

  • @Rovannlinhalis only a get; set; of the user type within the Entity address solves the problem? In the controller I put the Entity address receiving the list? This? Put as reply and I mark as correct. thanks!

  • Json is in the question Edit

No answers

Browser other questions tagged

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