I have two entities "YogaSpace" and "YogaSpaceImage" I'm deleting an image and trying to get the row removed, instead it just nulls out the reference to the YogaSpace entity.
Here are my entities.
public class YogaSpace
{
public int YogaSpaceId { get; set; }
public virtual ICollection<YogaSpaceImage> Images { get; set; }
}
public class YogaSpaceImage
{
public int YogaSpaceImageId { get; set; }
public byte[] Image { get; set; }
public byte[] ImageThumbnail { get; set; }
public string ContentType { get; set; }
public int Ordering { get; set; }
}
Here is the method that deleted one image from the db.
[HttpPost]
public ActionResult RemoveImage(string id, string imageId)
{
YogaSpace yogaSpace = yogaSpaceRepository.Find(Convert.ToInt16(id));
YogaSpaceImage image = yogaSpace.Images.FirstOrDefault(si => si.YogaSpaceImageId == Convert.ToInt16(imageId));
yogaSpace.Images.Remove(image);
yogaSpaceRepository.InsertOrUpdate(yogaSpace);
yogaSpaceRepository.Save();
return Json("removed");
}
here is what's inside my repo to insertorupdate()
public void InsertOrUpdate(YogaSpace yogaSpace)
{
if (yogaSpace.YogaSpaceId == default(int))
{
context.Entry(yogaSpace).State = System.Data.Entity.EntityState.Added;
}
else
{
context.Entry(yogaSpace).State = System.Data.Entity.EntityState.Modified;
}
}
and here is the result. you can see the row doesn't get deleted, null gets put into the reference column. I don't have a repo for YogaSpaceImage entity, only YogaSpace so trying to use it so I don't have to create another repo.