0

I am learning EF Code First with migrations, I have 3 entities :

[User] 1--->* [Call] 1--->* [ETA]

Code :

User.cs

public class User
{
    [Key]
    public int Id { get; set; }

    public Guid LongId { get; set; }

    [Required]
    public string Name { get; set; }

    public virtual ICollection<Call> Calls { get; set; } // many calls

    public User()
    {
        LongId = Guid.NewGuid();
    }
}

Call.cs

public class Call
{
    [Key]
    public int Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string BreakdownNo { get; private set; }

    [Required,MaxLength(32)]
    public string Customer { get; set; }

    [Required,MaxLength(32)]
    public string TrailerNo { get; set; }

    [Required, MaxLength(32)]
    public string DepotContact { get; set; }

    [Required, MaxLength(48), RegularExpression(@"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$")]
    public string DepotEmail { get; set; }

    [Required, MinLength(9), MaxLength(32)]
    public string DepotPhone { get; set; }

    [Required, MaxLength(32)]
    public string DriverContact { get; set; }

    [Required, MinLength(9), MaxLength(32), RegularExpression(@"^(7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$")]
    public string DriverPhone { get; set; }

    [Required, MaxLength(256)]
    public string LocatedAtFreeText { get; set; }

    [Required, MaxLength(8), RegularExpression(@"^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,1}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$")]
    public string LocatedAtPostCode { get; set; }

    [Required, MaxLength(16)]
    public string StartupNo { get; set; }

    [Required]
    public bool IsLoaded { get; set; }

    [Required, MaxLength(256)]
    public string FaultDescription { get; set; }

    [Required]
    public DateTime StartTime { get; set; }

    public DateTime? EndTime { get; set; }
    public string Status { get; set; }

    public virtual User Controller { get; set; } // 1 controller
    public virtual ICollection<ETA> ETAs { get; set; } // many ETAs

    public Call()
    {
        StartTime = DateTime.Now;
        ETAs = new List<ETA> { new ETA() };
        Status = "Logged";
    }
}

ETA.c

public class ETA
{
    [Key]
    public int Id { get; set; }

    [Required]
    public TimeSpan Value { get; set; }

    public int CallId { get; set; }

    public ETA()
    {
        Value = TimeSpan.FromMinutes(90);
    }
}

I would like it so when I delete the User it deletes all of the Calls for the User, which in turn deletes all of the ETAs for those Calls.

When I delete a User row from the Database (using database explorer) it gives me an error :

No rows were deleted. A problem occurred attempting to delete row 201. Error Source: .Net SqlClient Data Provider. Error Message: The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Calls_dbo.Users_Controller_Id". The conflict occurred in database "BreakdownDb", table "dbo.Calls", column 'Controller_Id'.

sprocket12
  • 5,368
  • 18
  • 64
  • 133

2 Answers2

0

You can turn on the Cascade Delete option in Entity Framework, here you will find more info: http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx

unjuken
  • 1,106
  • 9
  • 9
0

The solution was to add OnModelCreating method to my DbContext class :

public class BreakdownDb : DbContext
{
    public DbSet<Call> Calls { get; set; }
    public DbSet<User> Users { get; set; }

    public BreakdownDb(): base("name=DefaultConnection") {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasMany(x => x.Calls).WithRequired();
        modelBuilder.Entity<Call>().HasMany(x => x.ETAs).WithRequired();
    }
}
sprocket12
  • 5,368
  • 18
  • 64
  • 133