0
I have the following situation: my database is Mysql, when I try to delete a record directly in the database, 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, this is exactly what I need, however, when I try to delete by the application, it is not respected the On Delete restrict, which is set in the table creation. I am using Entity Framewrok 6. I understand that when trying to delete by the application, Mysql should trigger the same restriction exception for the application. Someone can help me?
here is the group class
[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 is the user 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
}
I didn’t understand. By Mysql he gives you the message, but by the system (EF) no?
– Randrade
That’s right, the system(EF) does not give the message and erases the record and its dependencies
– alessandre martins
I needed this information to better understand your problem. I hope the answer helps you.
– Randrade