I want to mark some record from detached object as deleted and then delete it from db, but get exception "Adding a link to an entity in the Deleted state is not allowed." How can I delete this nested record from db?
public static void UpdateCar(rentcar2.Models.Car i)
{
using (rentcar2.Dal.Entities db = new rentcar2.Dal.Entities())
{
rentcar2.Dal.Car cr = AutoMapper.Mapper.Map<rentcar2.Dal.Car>(i);
int a = 0;
foreach (rentcar2.Dal.CarImage c in cr.CarImages)
{
c.CarId = i.Id;
// fl 0-old_unmodified, 1-adding, 2-deleted, 3-modified
switch (i.CarImages[a].fl)
{//try to mark record
case 0:
db.Entry(c).State = EntityState.Unchanged;
break;
case 1:
db.Entry(c).State = EntityState.Added;
break;
case 2:
**db.Entry(c).State = EntityState.Deleted;**
break;
case 3:
db.Entry(c).State = EntityState.Modified;
break;
}
a++;
}
try
{
**db.Cars.Attach(cr);** // excpetion here
db.Entry(cr).State = EntityState.Modified;
db.SaveChanges();
}
catch (Exception e)
{
throw new System.Exception("", e);
}
}
}