Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code first cascade delete on foreign key one-to-many

We are working in Entity framework Code first

We have a class video

class Video{
   List<ImageInfo> Images{
      get; set;
   }
}

our image infoclass contains a path to the image and some other info

class ImageInfo{
    String path;
    ...
}

we would like to have EF remove the imageinfos when removing the video

so we changed the modelbuilder like follows:

modelBuilder
    .Entity<Video>()
    .HasMany(v => v.Images)
    .WithRequired()
    .WillCascadeOnDelete(true);

we don't want to add a link back to video in our imageinfo class.

is it possible to get cascade delete functionality without a 2 way foreign key?

EDIT

the video_id of the imageInfo doesn't get filled in in the database when saving a video.

http://pbrd.co/14X82vb

how can we fix this?

I don't know if its related, but when we add a new video with images at the same time, we get this error:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
like image 978
Alexander Cosman Avatar asked Feb 15 '13 15:02

Alexander Cosman


People also ask

How do I enable cascade delete in Entity Framework?

Cascade delete automatically deletes dependent records or sets null to ForeignKey columns when the parent record is deleted in the database. Cascade delete is enabled by default in Entity Framework for all types of relationships such as one-to-one, one-to-many and many-to-many.

How does foreign key on delete cascade work?

A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted.

What is DeleteBehavior Cascade?

DeleteBehavior.Cascade – Delete the child when the parent is deleted (e.g. Cascading deletes) DeleteBehavior.SetNull – Set the FK on the child to just be null (So allow orphans) DeleteBehavior.Restrict – Don't allow the parent to be deleted at all.

What is the function of 1 delete cascade?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.


1 Answers

The WithRequired introduces the 2 way relationship. So you should do the following.

modelBuilder
    .Entity<Video>()
    .HasMany(v => v.Imgages)
    .WithOptional()
    .WillCascadeOnDelete(true);

... and assuming you want the relationship the other way around ...

public class Video { }
public class ImageInfo {
    public virtual Video { get; set; }
}

modelBuilder
    .Entity<ImageInfo>()
    .HasRequired(v => v.Video)
    .WithMany()
    .WillCascadeOnDelete(true);

PS: I think the List<ImageInfo> should be virtual, so here is how I'd define it ...

public class Video {
    public Video() { this.Images = new List<ImageInfo>(); }
    public virtual ICollection<ImageInfo> Images { get; set; }
}
like image 162
AxelEckenberger Avatar answered Oct 13 '22 21:10

AxelEckenberger