加载实体时,删除级联时的实体框架 [英] Entity Framework On Delete Cascade when entity is loaded

查看:61
本文介绍了加载实体时,删除级联时的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im将Entity Framework 6 Code Firsts与MySQL结合使用。

im using Entity Framework 6 Code Firsts with MySQL.

我具有以下实体和流利的配置:

I have the following entities and fluent configurations:

    public class Cotizacion
{
    public int CotizacionId             { get; set; }
    public int Numero                   { get; set; }
    public DateTime Fecha               { get; set; }
    public int? ClienteId               { get; set; }
    public Cliente Cliente              { get; set; }
    public List<ItemsCotizacion> Items  { get; set; }

}

public class Cliente
{
    public int ClienteId                    { get; set; }
    public string RazonSocial               { get; set; }
    public string Cuit                      { get; set; }
    public string Direccion                 { get; set; }
    public string Telefono                  { get; set; }
    public List<Cotizacion> Cotizaciones    { get; set; }
}

public class ConfigCliente : EntityTypeConfiguration<Cliente>
{
    public ConfigCliente()
    {
        Property(c => c.RazonSocial).IsRequired().HasMaxLength(100);
        Property(c => c.Direccion).IsOptional().HasMaxLength(100);
        Property(c => c.Cuit).IsOptional().HasMaxLength(15);
        Property(c => c.Telefono).IsOptional().HasMaxLength(100);
        HasKey(c => c.ClienteId);
        HasMany(c => c.Cotizaciones).WithRequired(cot => cot.Cliente).WillCascadeOnDelete(true);
    }

}
public class ConfigCotizacion : EntityTypeConfiguration<Cotizacion>
{
    public ConfigCotizacion()
    {
        Property(c => c.Fecha).IsRequired();
        HasRequired(c => c.Items);
        HasMany(c => c.Items).WithRequired(i => i.Cotizacion).WillCascadeOnDelete(true);
        HasRequired(c => c.Cliente).WithMany(cot => cot.Cotizaciones);
    }   
}

当我删除实体 Cliente时,我希望这样做EF删除所有相关实体 Cotizacion。仅当我在上下文中加载列表Cotizaciones时,级联删除才会失败,但是当我不在上下文中加载Cotizaciones列表时,级联删除可以正常工作。

I want that when i delete an entity "Cliente" EF Deletes all the related entities "Cotizacion" .The cascade delete fails ONLY when i load the List Cotizaciones in the context , but when i dont load the Cotizaciones list in the context, the cascade delete works fine.

我得到以下错误:


{无法添加或更新子行:外键约束失败
(\ pruebaentity\ .\ cotizacion\,CONSTRAINT
\ FK_Cotizacion_Cliente_ClienteId\ FOREIGN KEY(\ ClienteId\)
参考\ cliente\(\ ClienteId\)在更新
CASCADE时删除级联)}

{"Cannot add or update a child row: a foreign key constraint fails (\"pruebaentity\".\"cotizacion\", CONSTRAINT \"FK_Cotizacion_Cliente_ClienteId\" FOREIGN KEY (\"ClienteId\") REFERENCES \"cliente\" (\"ClienteId\") ON DELETE CASCADE ON UPDATE CASCADE)"}


推荐答案

已解决:
问题是我使用了

Solved: The problem was that i used

context.Entry(entity).Sate = System.Data.Entity.EntityState.Deleted

而不是

context.Clients.Remove(entity)

这篇关于加载实体时,删除级联时的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆