Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载实体时删除级联上的实体框架_C#_Mysql_Entity Framework - Fatal编程技术网

C# 加载实体时删除级联上的实体框架

C# 加载实体时删除级联上的实体框架,c#,mysql,entity-framework,C#,Mysql,Entity Framework,我在MySQL中使用EntityFramework6代码 我有以下实体和配置: public class Cotizacion { public int CotizacionId { get; set; } public int Numero { get; set; } public DateTime Fecha { get; set; } public int? Cl

我在MySQL中使用EntityFramework6代码

我有以下实体和配置:

    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);
    }   
}
我希望当我删除一个实体客户时,EF会删除所有相关的实体协同工作。级联删除仅在我在上下文中加载列表协同工作时失败,但当我在上下文中不加载协同工作列表时,级联删除工作正常

我得到以下错误:

{无法添加或更新子行:外键约束失败 \pruebaentity\。\cotizacion\,约束 \FK_Cotizacion_Cliente_clientId\FOREIGN KEY\clientId\ 更新时删除级联时引用\cliente\\ClienteId\ 级联}

已解决: 问题是我用了

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

context.Clients.Remove(entity)