1
Good morning, I have the following code below:
public void AdicionarAlunoTurma(Adicionar_Alunos aluno)
{
List<string> parametros = new List<string>();
StringBuilder query = new StringBuilder();
parametros.Add(aluno.id.ToString());
parametros.Add(aluno.nome);
parametros.Add(aluno.turmaID.ToString());
query.Append("insert into persons_turma (personsID, persons_turma, turmaID) values (@param1,@param2,@param3) ");
SqlDynamic.Execute(db, query.ToString(), parametros.ToArray());
and the table fields are these.
I defined people as unique because they were having problems with duplication. How could I be adding the same student to another class? I tried to develop an IF before the query, causing SQL to ignore that it was unique and add anyway. but I couldn’t finish this logic.
Thanks in advance for the help
Adding the column
turmaID
in the key composition?– Leandro Angelo
i would select previously, to check if personId already exists when it comes to the class, if the result is greater than 1 then the user has already been registered in that class!
– Lodi
The personId cannot be unique, the table persons_turma is to make the relation of students with the class, the table persons_turma must have only the fields Personid (not single, foreign key referencing a Personid of the table Person) , Turmaid (not single, foreign key referencing Class table Turmaid) is a primary key Personturmaid, so you ensure that you make the list of existing people with existing classes and can reference anyone with any class
– João Paulo Amorim
There is probably a problem of poor table definition and relationships as said by @Joãopauloamorim in the comment above. Post the create command of the table persons_turma so we can analyze better, I would even answer something, plus it seems that the definition of the keys and relationships of the table is in trouble.
– Julio Borges
I didn’t save the codes used to create the table (I created it about 5 days ago)
– user139372
I removed that command from him to be ONLY. I would only need him to do a scan to see if the student is already in that class
– user139372
@Matheusmuniz make a select in the table before passing personId and Turmaid, if return records you do not save.
– João Paulo Amorim
@Joãopauloamorim I made the change and made the following code:
– user139372