I have two related tables like this :
Users
public partial class Users
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Users()
{
this.Orders = new HashSet<Orders>();
}
public int ID { get; set; }
public int UserType_ID { get; set; }
public string Email { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Orders> Orders { get; set; }
public virtual UserTypes UserTypes { get; set; }
}
Oders
public partial class Orders
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Orders()
{
this.Payments = new HashSet<Payments>();
}
public int ID { get; set; }
public int User_ID { get; set; }
public int Plan_ID { get; set; }
public System.DateTime Date { get; set; }
public int OrderStatus_ID { get; set; }
public virtual OrderStatus OrderStatus { get; set; }
public virtual Plans Plans { get; set; }
public virtual Users Users { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Payments> Payments { get; set; }
}
Now i want force entity framework to cascade delete user rows.
Here is codes for delete a user :
using (Crypto_Shuffler_Entities entities = new Crypto_Shuffler_Entities())
{
var users = from User in entities.Users
where User.Email.ToLower() == "test@gmail.com"
select User;
entities.Users.Remove(users.FirstOrDefault());
entities.SaveChanges();
}
An here is error :
An error occurred while updating the entries. See the inner exception for details.
Message = "The DELETE statement conflicted with the REFERENCE constraint "FK_Orders_Users". The conflict occurred in database "Crypto_DB", table "dbo.Orders", column 'User_ID'.\r\nThe statement has been terminated."
How force entity framework for cascade delete?