简体   繁体   中英

Removing child entity not deleting in database entity framework

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. 在此处输入图片说明

You need to mark the entity as deleted.

yogaSpace.Images.Remove(image);
context.YogaSpaceImages.Remove(image)

your other option is to mark you mapping with .WillCascadeOnDelete()

Eg

protected override void OnModelCreating(DbModelBuilder mb)
{

    mb.Entity<YogaSpace>().HasMany(p => p.Images )
        .WithRequired()
        .HasForeignKey(c => c.YogaSpaceImageId)
        .WillCascadeOnDelete();

} 

The other option is to have a ForeignKey on the YogaSpaceImage back to the YogaSpace (if it is a mapping of 1 YogaSpace - 0-many Image ) and mark the YogaSpaceImage.YogaSpace foreign key required. Therefore whenever EF tried to remove the foreign key, it will automatically delete the Image.

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; }

    public int YogaSpaceId { get; set; }
    [Required] <-- This will delete the image, if removed from parent
    public virtual YogaSpace YogaSpace { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM