我有一个具有一对多关系的聚合实体的子实体,并且在子实体中,有一个包含父代ID的列表。来自前端的数据是一个对象列表,如果与后端没有什么不同,我将不做任何处理。否则,我将删除已删除的内容并添加已添加到表中的内容。
我是EF CORE的新手,我正尝试在这种关系中应用此登录名。
if (child.list.SuccessorId == parent.vehicleCategoryId) => ignore;
if(!child.list.contain(parent.vehicleCategoryId)
remove(parent.vehicleCategoryId) => //delete record with vehicleCategoryId
else
add(child)
这是我的实体。
public class VehicleCategory : LookupAggregateRoot<VehicleCategory>
{
#region Constructor
private VehicleCategory()
{
_successors = new List<VehicleSuccessorCategory>();
}
#endregion
#region Data
public virtual LocalizedText Name { get; set; }
public virtual long Sequence { get; set; }
private readonly List<VehicleSuccessorCategory> _successors;
public IEnumerable<VehicleSuccessorCategory> Successors
{
get => _successors.AsReadOnly();
set => throw new NotImplementedException();
}
#endregion
#region Behaviour
public void AddSuccessor(VehicleSuccessorCategory entrySuccessorCategory)
{
_successors.Add(entrySuccessorCategory);
}
public void RemoveSuccessor(VehicleSuccessorCategory entrySuccessorCategory)
{
_successors.Remove(entrySuccessorCategory);
}
}
public class VehicleSuccessorCategory : ID365Entity<int>, IEnumerable
{
#region Constructor
public int Id { get; set; }
public int SuccessorId { get; set; }
public VehicleSuccessorCategory(int order)
{
Order = order;
}
#endregion
#region Data
public int Order { get; set; }
#endregion
public bool IsTransient()
{
throw new NotImplementedException();
}
public IEnumerator GetEnumerator()
{
yield return Id;
yield return Order;
}
}
我尝试了
VehicleCategory vehicleCategory = _genericRepository.Get(aggregate.Id);
foreach (var successorCategory in aggregate.Successors)
{
var successorCategoryToRemove =
vehicleCategory.Successors.Where(e => e.SuccessorId == successorCategory.SuccessorId);
foreach (var vehicleSuccessorCategory in successorCategoryToRemove)
vehicleCategory.RemoveSuccessor(vehicleSuccessorCategory);
}