Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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# 实体框架6:删除多个父项的子项_C#_Entity Framework_Ef Code First_Entity Framework 6_Code First - Fatal编程技术网

C# 实体框架6:删除多个父项的子项

C# 实体框架6:删除多个父项的子项,c#,entity-framework,ef-code-first,entity-framework-6,code-first,C#,Entity Framework,Ef Code First,Entity Framework 6,Code First,请帮忙。我使用EntityFramework 6。拥有实体: public class PowerStation { public Guid PowerStationId { get; set; } public string Name { get; set; } public Guid PowerStationTypeId { get; set; } public virtual PowerStationType PowerS

请帮忙。我使用EntityFramework 6。拥有实体:

public class PowerStation {

        public Guid PowerStationId { get; set; }
        public string Name { get; set; }

        public Guid PowerStationTypeId { get; set; }
        public virtual PowerStationType PowerStationType { get; set; }

        public Guid SubjectId { get; set; }
        public DateTime SubjectTransactionTime { get; set; }
        public virtual Subject Subject { get; set; }

        public Guid ParticipantOremId { get; set; }
        public DateTime ParticipantOremTransactionTime { get; set; }
        public virtual ParticipantOrem ParticipantOrem { get; set; }

        public Guid? DcId { get; set; }
        public DateTime? DcTransactionTime { get; set; }
        public virtual Dc Dc { get; set; }

        public virtual ICollection<Equipment> Equipments { get; set; }
        public virtual ICollection<DcLink> DcLinks { get; set; }   
        public virtual ICollection<DcPowerStationProposal> DcPowerStationProposals { get; set; }
}

如果你想删除整个PowerStation,你应该删除迁移并重新更新它

我想删除数据库中的行,而不是表。我没有检查代码,但它说你的表用外键连接了其他表,它们不可为空,使它们为空或删除连接的行?我尝试删除连接的行,但没有级联删除,没有帮助。可以为空-这是个问题,但我尝试了-没有结果。请使用我,可以为空的帮助。。。但我认为,这对我来说是不正确的。
entity = db.PowerStations
                    .Include(x => x.Equipments)
                    .Include(x => x.DcLinks)
                    .Include(x => x.DcPowerStationProposals)
                    .FirstOrDefault(x => x.PowerStationId == entity.PowerStationId && x.TransactionTime == entity.TransactionTime);


var station = db.ParticipantOrems.FirstOrDefault(x => x.ParticipantOremId == entity.ParticipantOremId
                && x.TransactionTime == entity.ParticipantOremTransactionTime);
                station.PowerStations.Remove(entity);

var subject = db.Subjects.FirstOrDefault(x => x.SubjectId == entity.SubjectId
                && x.TransactionTime == entity.SubjectTransactionTime);
                subject.PowerStations.Remove(entity);

var dc = db.Dcs.FirstOrDefault(x => x.DcId == entity.DcId
                && x.TransactionTime == entity.DcTransactionTime);
                dc.PowerStations.Remove(entity);

var type = db.PowerStationTypes.FirstOrDefault(x => x.PowerStationTypeId == entity.PowerStationTypeId);
                type.PowerStations.Remove(entity);

db.Equipments.Local.Where(x => x.PowerStation == null).ToList().ForEach(r => Equipments.Remove(r));
db.DcLinks.Local.Where(x => x.PowerStation == null).ToList().ForEach(r => DcLinks.Remove(r));
db.PowerStationProposals.Where(x => x.PowerStation == null).ToList().ForEach(r => DcPowerStationProposals.Remove(r));