Entity Framework Code First Cascading Delete One To Many

Viewed 4715

I am attempting to create a small demo solution to experiment with EF CF Cascading deletes.

With the code I have written I am getting the following error when trying to add a Person with 2 cars.

The aim is to add a person with 2 cars. Then delete the person and linked cars get deleted at the same time.

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[EF_Cascading_Delete_Experiment.Car]' to type 'EF_Cascading_Delete_Experiment.Car'.

I am trying to build a simple example where there is a Person with a list of Cars

Here are my Person & Car Classes:

public class Person
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Car> Cars { get; set; }
}
public class Car
{
    [Key]
    public int id { get; set; }
    public string CarName { get; set; }
}

Here is my simplistic code trying to ad a Person with 2 cars:

public static void CarTest()
    {
        using (Model1 db = new Model1())
        {
            Person personToAdd = new Person();
            personToAdd.Name = "trev";
            personToAdd.Cars = new List<Car>();

            Car car1 = new Car
            {
                CarName = "Vectra"
            };

            Car car2 = new Car
            {
                CarName = "Focus"
            };

            personToAdd.Cars.Add(car1);
            personToAdd.Cars.Add(car2);

            db.Person.Add(personToAdd);

            db.SaveChanges();
        }
    }

The error occurs at the line

db.Person.Add(personToAdd);

This is my DbContext:

public class Model1 : DbContext
{
    // Your context has been configured to use a 'Model1' connection string from your application's 
    // configuration file (App.config or Web.config). By default, this connection string targets the 
    // 'EF_Cascading_Delete_Experiment.Model1' database on your LocalDb instance. 
    // 
    // If you wish to target a different database and/or database provider, modify the 'Model1' 
    // connection string in the application configuration file.
    public Model1()
        : base("name=Model1")
    {
    }

    // Add a DbSet for each entity type that you want to include in your model. For more information 
    // on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.

    public virtual DbSet<Person> Person { get; set; }
    public virtual DbSet<Car> Car { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>()
            .HasOptional(a => a.Cars)
            .WithOptionalDependent()
            .WillCascadeOnDelete(true);
    }
}

The migration code generated by EF looks like this:

public partial class addedbackeverythingincludingcascadingdelete : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Cars",
            c => new
                {
                    id = c.Int(nullable: false, identity: true),
                    CarName = c.String(),
                })
            .PrimaryKey(t => t.id);

        CreateTable(
            "dbo.People",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Cars_id = c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Cars", t => t.Cars_id, cascadeDelete: true)
            .Index(t => t.Cars_id);

    }

    public override void Down()
    {
        DropForeignKey("dbo.People", "Cars_id", "dbo.Cars");
        DropIndex("dbo.People", new[] { "Cars_id" });
        DropTable("dbo.People");
        DropTable("dbo.Cars");
    }
}

To me, it looks like the migration code is not correct? Which would have been generated based on my Person & Car class. But I cannot work out why?

When I look at the tables in the database they look wrong.

enter image description here

Surely in the Car table there should be a PersonId? Not a CarId in the Person table?

SOLUTION:

With many thanks to Ivan this was my solution. I have put it here so I can mark his question as the answer.

My classes now look like this:

public class Person
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<Car> Cars { get; set; }
}
public class Car
{
    [Key]
    public int id { get; set; }
    public string CarName { get; set; }
}

When testing, even though Ivan says I shouldn't need it I found the cascading delete wouldn't work unless I kept the this code:

modelBuilder.Entity<Person>()
            .HasMany(a => a.Cars)
            .WithOptional() // or `WithRequired() in case Car requires Person
            .WillCascadeOnDelete(true);
3 Answers

The fluent relationship configuration

modelBuilder.Entity<Person>()
    .HasOptional(a => a.Cars)
    .WithOptionalDependent()
    .WillCascadeOnDelete(true);

is wrong. HasOptional, HasRequired, WithOptionalDependent, WithOptionalPrincipal etc. are for one-to-one relationships, while you have one-to-many.

The correct configuration is as follows:

modelBuilder.Entity<Person>()
    .HasMany(a => a.Cars)
    .WithOptional() // or `WithRequired() in case Car requires Person
    .WillCascadeOnDelete(true);

Now the migration should look like this:

CreateTable(
    "dbo.People",
    c => new
        {
            Id = c.Int(nullable: false, identity: true),
            Name = c.String(),
        })
    .PrimaryKey(t => t.Id);

CreateTable(
    "dbo.Cars",
    c => new
        {
            id = c.Int(nullable: false, identity: true),
            CarName = c.String(),
            Person_Id = c.Int(),
        })
    .PrimaryKey(t => t.id)
    .ForeignKey("dbo.People", t => t.Person_Id, cascadeDelete: true)
    .Index(t => t.Person_Id);

Actually You haven't added Person property in your car model class. Your model should be like this.

See Here. Cascade delete in one to many relationship

public class Person
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Car> Cars { get; set; }
}
public class Car
{
    [Key]
    public int id { get; set; }
    public string CarName { get; set; }
    public virtual Person Person { get; set;}
}

Now run migration. And Test it.

First, i think it's better to add the virtual keyword in Person class (for lazy loading) :

public virtual List<Car> Cars { get; set; }

You can add a reference to the Person on the car class with required annotation :

[Required]
public virtual Person Person { get; set; }

since a person is required, deleting a person will cascade delete all his cars.

Related