I am using WCF Rest service. I am trying to delete multiple records from the repository called “_altProductVersionEntityRepository”
Here is my entire code below
public Result UpdateProductObject(ProductObjectToSave prodSave)
{
IUnitOfWork unitOfWork = new UnitOfWork((IObjectContext)_objectSetFactory);
var versions = prodSave.VersionDetails;
var altVersions = prodSave.AlternateVersionDetails;
foreach (var versionDetail in versions)
{
var detail = versionDetail;
var dbVersionentity = _productVersionEntityRepository.FirstOrDefault(x => x.Id == detail.Id);
var altVersionEntity = _altProductVersionEntityRepository.FirstOrDefault(x => x.ProductVersionEntityId == detail.Id);
if (dbVersionentity == null)
{
dbVersionentity = new ProductVersionEntity();
_productVersionEntityRepository.Insert(dbVersionentity);
dbVersionentity.Id = GetNextTableId("vProductVersion");
dbVersionentity.CreatedOn = DateTime.Now;
}
dbVersionentity.Name = detail.Name;
dbVersionentity.Code = detail.Code;
if (detail.Id > 0){
_productVersionEntityRepository.Update(dbVersionentity);
if (altVersionEntity != null){
_altProductVersionEntityRepository.Delete(altVersionEntity);
}
}
}
try
{
unitOfWork.Commit();
}
catch (Exception e)
{
return new Result() { Status= e.Message };
}
return new Result() { Status= = "Record updated successfully" };
}
In variable “altVersionEntity” i try to find first record from “_altProductVersionEntityRepository” repository. In this repository there are multiple records but it only delete single record as i am doing FirstOrDefault here. What is way to delete all records. I am using FirstOrDefault because i want to delete records only if they are find otherewise it will throw error.
What is way to delete multiple records? I am still new to WCF so please help me.
I have find my own solution. What i do i use find method instead of FirstOrDefault which i was using in above code. I use find method like below
Then i use variable altVersionEntity and convert to list and then loop through it and delete records from “altProductVersionEntityRepository” repository one by one like below