0

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?

SilverLight
  • 19,668
  • 65
  • 192
  • 300

1 Answers1

-1

i think you need to remove first ref of users like in orders here one to many relation so according to my opinion first remove users in order and then save it to db and after that remove this user's row this may not be conflict

theKing
  • 11
  • 3
  • Thanks for the answer, But my tables are for example. How can i force EF to delete related order records auto? For more tables it is difficult to delete each related individually. – SilverLight Jan 12 '22 at 11:28
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 12 '22 at 12:22