In this case, it would be feasible to record the header, perform the recovery of the last recorded ID for the user and write the Items or it would be better to do a precedent and leave this responsibility with the database?
If you are using Entity Framework, you do not need to do this. You can do it as follows:
var modalidade = db.Modalidades.FirstOrDefault();
var usuario = /* Recupere aqui seu usuário */
var cabecalho = new Cabecalho
{
Modalidade = modalidade,
Usuario = usuario,
ValorJogado = 10,
HoraJogo = DateTime.Now,
Detalhes = new List<Detalhe>
{
new Detalhe { NumeroJogo = 3 }
}
};
db.Cabecalhos.Add(cabecalho);
db.SaveChanges(); // Grava cabecalho e o detalhe automaticamente.
By ADO.NET, you may have to use transactional scope and command two insertion operations and a selection between them (bring the header record and set the FK in detail).
@Ricardo, friend, client server does not have 3G, it is to make the recording through a mobile, sending the data by a Web API made in C#
– Harry
Cellular = client, Web API (runs on server) = server. Nomenclature only. Nothing wrong with using Precedent
– Ricardo
@Ricardo, so you think you better make the creation of the trial because there might be some problem of losing data or do you have another justification? just for the better understanding. I thank you!
– Harry
I deleted everything, I said nonsense. You can create the Precedent, but you can also make your application server make the move with ID. What the phone has to do is send everything at once to the server. Now I said right :)
– Ricardo
@Ricardo ok! I appreciate the help!
– Harry
And about data loss, if the application server is doing no loss, just be in transaction.
– Ricardo
@Ricardo, you meant that when sending all the data, the processing will be done in the Controller and this way will not have data loss.
– Harry
That’s right. And you will only have one call to the server. Best case scenario for the phone, The API must have a URL to receive all of them.
– Ricardo
@Ricardo, now you’ve given me a "cat jump" tip to api getting everything together, problem is knowing how many numbers will have in detail
– Harry
@itasouza, just one question. your application will run on a mobile device ?
– Marco Souza
@Marconciliosouza , yes!
– Harry
@itasouza, in this case the ideal is to have web services for communication between your device and the database, in the case of your tables the best is to create two Process one for each table for the following reason, imagine that you will record more than one detail for each header (1 => N) then the parameters would be the same for a new detail.
– Marco Souza
You are using Entity Framework or ADO.NET?
– Leonel Sanches da Silva