Search code examples
c#entity-frameworkentity-framework-6cascading-deletes

Entity Framework: how to delete related entity upon update or delete of 'generic' parent


I have a Product class, which contains a Picture property. Other classes may also have a picture, e.g. a Customer can have a Picture.

Product.cs:

public class Product
{
    public Picture Picture { get; set; }
} 

Customer.cs:

public class Customer
{
    public Picture Picture { get; set; }
} 

Picture.cs:

public class Picture
{
    public string Type { get; set; }

    public byte[] Content { get; et; }
}

The picture is always optional, i.e. Products and Customers may or may not have a picture. However, a picture is uniquely linked to exactly one 'parent' entity (either a Product or a Customer). Without the parent, there's no use for the Picture to exist. Note that there's no link from Picture to the parent class, because this parent can be of multiple types (Product or Customer).

Another requirement is that I want to have full control over whether the picture is loaded or not, through eager or lazy loading. E.g. when retrieving a list of products, I don't want the picture to be fetched, but if a single product is requested, the picture should be included.

My question: how can I configure Entity Framework so that:

  • The Picture record is deleted when the parent (product or customer) record is deleted
  • The Picture record is deleted whenever the parent record is updated and the new version does not contain a picture
  • The Picture record is replaced (delete + create of a record) whenever the parent is updated with a new picture
  • The picture is optional
  • Always keep control over whether the picture is loaded or not

I'm using fluent API to define the relationship. Currently, there's no cascading delete whatsoever.

public class ProductMap : EntityTypeConfiguration<Product>
{
    public ProductMap()
    {
        ToTable("PRODUCTS");

        HasKey(x => x.Id);

        HasOptional(x => x.Picture).WithOptionalDependent().Map(m => m.MapKey("PICTUREID"));
    }
}

I tried using WithRequired but this generates errors because there's no link or foreign key from Picture to Product/Customer.


Solution

  • you can do as follow:

    public abstract class Picture
    {
        public int Id {get;set;}
        public string Type { get; set; }
        public byte[] Content { get; set; }
    }
    
    public class ProductImage:Picture
    {
        public int ProductId {get;set;}
        public virtual Product Product {get;set;}
    }
    public class CustomerImage:Picture
    {
       public int CustomerId {get;set;}
       public virtual Customer Customer{get;set;}
    }
    

    then you can configuer like this: ex. for product:

    HasOptional(x=>x.ProductImage)
     .withRequired(x=>x.Product)
     .HasForeignKey(x=>x.ProductId); //cascade on delete is default true
    

    In this way you can load picture when you need it, and if you delete product item, the image will be deleted as well. The image is optional, and can be replaced. On update you must specified a new image or delete the old image.

    Hope this alternative will help you chose exact what you want.