How To Prevent duplicates Entries in Database using Telerik MVC Kendo Grid and LINQ?
public ActionResult Drzave_Create([DataSourceRequest]DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<DrzaveViewModel> drzave)
// Will keep the inserted entitites here. Used to return the result later.
var entities = new List<Drzave>();
if(ModelState.IsValid)
using(var autoOtpad = new AutoOtpadEntities())
var product = new List<Drzave>();
foreach(var drzava in drzave)
// Create a new Product entity and set its properties from the posted ProductViewModel
var entity = new Drzave
DrzavaId = drzava.DrzavaId,
Naziv = drzava.Naziv,
PuniNaziv = drzava.PuniNaziv
;
var query = autoOtpad.Drzave.Where(x => x.Equals(product).ToList();
if (query != null)
// row exists
else
// update DB// Add the entity
autoOtpad.Drzave.Add(entity);
// Store the entity for later use
entities.Add(entity);
// Insert the entities in the database
autoOtpad.SaveChanges();
// Return the inserted entities. The grid needs the generated ProductID. Also return any validation errors.
return Json(entities.ToDataSourceResult(request, ModelState, drzava => new DrzaveViewModel
DrzavaId = drzava.DrzavaId,
Naziv = drzava.Naziv,
PuniNaziv = drzava.PuniNaziv
));
I have a problem I need to remove entering duplicates to database on web form, but dont know solution yet, anyone? Please need help big time...