how to avoid data duplication

0

I need that before executing the data insertion to sql that I am consulted if there is the same data in this case I have a field called cedula which is the one I intend to use for the verification but I do not know how to perform the query. HELP PLEASE

    
asked by Samuel Ignacio Susana Confesor 13.06.2017 в 00:41
source

2 answers

1

Simply put the unique attribute (UQ) in the field in the database, and it will no longer let you put an equal record. From the front you must make a query of the user that the document number resembles the one they send, it is a query more or less like this, "Select user from table where id like% iddelfront%"; then if that query returns something it is because there is a record, if it returns something will not save anything, that validation can be done in the saved controller that you have, that if you want an error message to appear to the user in the frontend, because just by putting the attribute in the field you would already have. I hope it serves you.

    
answered by 13.06.2017 в 00:51
0

Also, as you were told, you can control the way in which the data is stored, always asking if it already exists in the BD example:

        var duplicado = db.Trabajador.Any(a => a.TrabajadorId == ObjetoParametro.TrabajadorId);

        if (duplicado)
         {            
            ModelState.AddModelError("TrabajadorId", "Ya existe este trabajador");
         }
          else
           {
                //Creas el trabajador
                db.SaveChanges();                   
           }
    
answered by 13.06.2017 в 04:44