cascade delete with entity framework

0

I have the following situation: my database is MySql, when I try to delete a direct record in the bank, from this message:

Cannot delete or update a parent row: a foreign key constraint fails ('lifeproject'.'t0041_usuario', CONSTRAINT 'fk_t0041_usuario_t0040_grupo_usuario1' FOREIGN KEY ('t0040_id_grupo') REFERENCES 't0040_grupo_usuario' ('t0040_id_grupo') ON UPDATE CASCADE)

Ok, this is correct, that's exactly what I need, but when I try to delete by the application, the On Delete restrict, which is set in the table creation, is not respected. I'm using Entity Framewrok 6. I understand that when trying to delete by the application, Mysql should fire the same constraint exception for the application. Can someone give me some help?

Here is the group classUsers

[Table("t0040_grupo_usuario")]
public class GrupoUsuarioModel
{
    #region propriedades
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string t0040_id_grupo { get; set; }

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

    [Required]
    public int t0020_id_empresa { get; set; }

    [ForeignKey("t0020_id_empresa")]
    public virtual EmpresaModel EmpresaModel { get; set; }


    public virtual ICollection<UsuarioModel> UsuarioModel { get; set; }        
    public virtual ICollection<AcessoModel> AcessoModel { get; set; }
    #endregion
}

}

Here are the users class

 [Table("t0041_usuario")]
public class UsuarioModel
{
    #region propriedades       
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int t0041_id_usuario { get; set; }

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

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

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

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

    [Required]
    public int t0020_id_empresa { get; set; }

    [ForeignKey("t0020_id_empresa")]
    public virtual EmpresaModel EmpresaModel { get; set; }

    [ForeignKey("t0040_id_grupo")]
    public virtual GrupoUsuarioModel GrupoUsuarioModel { get; set; }
    #endregion
}
    
asked by anonymous 28.05.2016 / 02:30

1 answer

1

The Entity Framework uses some conventions in its structure. Cascade Delete is one of them.

There are a few ways to remove this convention. The first, and simplest, is in the DbContext context itself, where you can add the following code:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            base.OnModelCreating(modelBuilder);
        }

In this way, we are talking about removing the Cascade Delete convention in OneToMany relationships, or 1: N relationships.

There are other options, such as ManyToManyCascadeDeleteConvention , for example.

Another way, if you are using Enfity Framework's Fluent Api , is to annotate the configuration file, like this:

modelBuilder.Entity<>(GrupoUsuarioModel)
            .HasOptional(p => p.UsuarioModel)
            .WithMany()
            .WillCascadeOnDelete(false);//true para adicionar

Remembering that case should use this way, you should perform the configuration on each mapping.

This article explains a little more about this, including option in the database (Sql Server).

Another point worth emphasizing is that there are several conventions, such as PluralizingTableNameConvention , which is responsible for "pluralizing" the table names, usually by adding the es suffix to the tables.

See the full list in the documentation official.

    
30.05.2016 / 14:30