Entity Framework 6:删除多个父项的子项
Entity Framework 6: Delete Child item for several parent
请帮忙。我和 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; }
}
我尝试删除这个实体,但出现异常:
The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.
我可以试试这个:
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));
如果您想删除整个 PowerStation,您应该删除迁移并重新更新它
请帮忙。我和 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; }
}
我尝试删除这个实体,但出现异常:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
我可以试试这个:
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));
如果您想删除整个 PowerStation,您应该删除迁移并重新更新它