简体   繁体   English

删除未在数据库实体框架中删除的子实体

[英]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. 我有两个实体“ YogaSpace”和“ YogaSpaceImage”我正在删除图像并尝试删除该行,相反,它只是使对YogaSpace实体的引用无效。

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() 这是我回购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. 您会看到该行没有被删除,null被放入引用列。 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. 我没有YogaSpaceImage实体的存储库,只有YogaSpace,因此可以尝试使用它,因此不必创建另一个存储库。 在此处输入图片说明

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() 您的另一个选择是使用.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. 另一个选项是将YogaSpaceImage上的ForeignKey返回YogaSpace (如果它是1 YogaSpace -0-many Image的映射),并标记出YogaSpaceImage.YogaSpace外键。 Therefore whenever EF tried to remove the foreign key, it will automatically delete the Image. 因此,每当EF尝试删除外键时,它将自动删除图像。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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