如何在 asp.net 核心 2 collection 实体中 soft-delete 记录?
How to soft-delete records in entity with collection in asp.net core 2?
我的问题是我无法在 collection SavePartnerRegistryResource
中 soft-delete 个实体。这些是我的实体资源,
public class SavePatientsRegistryResource {
public long? PatientFileId { get; set; }
public SavePatientsRegistryResource () {
Partners = new Collection<SavePartnerRegistryResource> ();
}
}
public class SavePartnerRegistryResource {
public string RecordId { get; set; }
public long? PatientFileId { get; set; }
public long? PartnerFileId { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
}
我这样做的方式是我急切地将我的实体加载到我的控制器中
我使用 automapper
从我的资源映射到我的实体。我在 AfterMap
、
中处理我的 collection (add/remove)
.AfterMap((s, d) = >{
foreach(PartnerRegistry partner in d.Partners.ToList()) {
SavePartnerRegistryResource modelPartner = s.Partners.FirstOrDefault(c = >c.PatientFileId == partner.PatientFileId && c.PartnerFileId == partner.PartnerFileId);
if (!s.Partners.Contains(modelPartner)) {
d.Partners.Remove(partner);
}
}
删除 SavePatientsRegistryResource
将软删除实体本身,而 collection BUT 从 collection SavePartnerRegistryResource
中删除将硬删除记录而不是将其标记为 soft-delete。两个实体在构建模型时都会收到 soft-delete 属性。
我在这里错过了什么?
我的覆盖保存方法是,
public override Task < int > SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken =
default(CancellationToken)) {
OnBeforeSaving();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
private void OnBeforeSaving() {
foreach(var entry in ChangeTracker.Entries()) {
if (!EntitiesToExclude().Contains(entry.Entity.ToString())) {
switch (entry.State) {
case EntityState.Added:
entry.CurrentValues["IsDeleted"] = false;
break;
case EntityState.Deleted:
entry.State = EntityState.Modified;
entry.CurrentValues["IsDeleted"] = true;
foreach(var navigationEntry in entry.Navigations.Where(n = >!n.Metadata.IsDependentToPrincipal())) {
if (navigationEntry is CollectionEntry collectionEntry) {
foreach(var dependentEntry in collectionEntry.CurrentValue) { (Entry(dependentEntry)).State = EntityState.Modified;
(Entry(dependentEntry)).CurrentValues["IsDeleted"] = true;
}
} else {
var dependentEntry = navigationEntry.CurrentValue;
if (dependentEntry != null) { (Entry(dependentEntry)).State = EntityState.Modified;
(Entry(dependentEntry)).CurrentValues["IsDeleted"] = true;
}
}
}
break;
}
}
}
}
我的解决方案是,
在映射过程中忽略集合,我删除了集合
AfterMap
.
中的修改
我使用 AutoMapper.Collection
创建了 EqualityComparison
地图
CreateMap<SavePartnerRegistryResource, PartnerRegistry> ().EqualityComparison ((s, d) => s.PatientFileId == d.PatientFileId && s.PartnerFileId == d.PartnerFileId);
我将集合映射到一个单独的 List
,
List<PartnerRegistry> patientPartnersModelDataList = mapper.Map<List<SavePartnerRegistryResource>, List<PartnerRegistry>> (model.Partners.ToList (), patient.Partners.ToList ());
现在我可以得到 added/removed/modified 个实体如下,
IEnumerable<PartnerRegistry> partnerRegistryToRemoveList = patient.Partners.ToList ().Except (patientPartnersModelDataList);
IEnumerable<PartnerRegistry> partnerRegistryToAddList = patientPartnersModelDataList.Except (patient.Partners.ToList ());
IEnumerable<PartnerRegistry> partnerRegistryToUpdateList = patient.Partners.ToList ().Intersect (patientPartnersModelDataList.ToList ());
然后我 运行 foreach
遍历每个实体并使用上下文执行 Add/Remove 而不是修改 collection
,
为了更新,我从 partnerRegistryToUpdateList
更新集合
我的问题是我无法在 collection SavePartnerRegistryResource
中 soft-delete 个实体。这些是我的实体资源,
public class SavePatientsRegistryResource {
public long? PatientFileId { get; set; }
public SavePatientsRegistryResource () {
Partners = new Collection<SavePartnerRegistryResource> ();
}
}
public class SavePartnerRegistryResource {
public string RecordId { get; set; }
public long? PatientFileId { get; set; }
public long? PartnerFileId { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
}
我这样做的方式是我急切地将我的实体加载到我的控制器中
我使用 automapper
从我的资源映射到我的实体。我在 AfterMap
、
.AfterMap((s, d) = >{
foreach(PartnerRegistry partner in d.Partners.ToList()) {
SavePartnerRegistryResource modelPartner = s.Partners.FirstOrDefault(c = >c.PatientFileId == partner.PatientFileId && c.PartnerFileId == partner.PartnerFileId);
if (!s.Partners.Contains(modelPartner)) {
d.Partners.Remove(partner);
}
}
删除 SavePatientsRegistryResource
将软删除实体本身,而 collection BUT 从 collection SavePartnerRegistryResource
中删除将硬删除记录而不是将其标记为 soft-delete。两个实体在构建模型时都会收到 soft-delete 属性。
我在这里错过了什么?
我的覆盖保存方法是,
public override Task < int > SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken =
default(CancellationToken)) {
OnBeforeSaving();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
private void OnBeforeSaving() {
foreach(var entry in ChangeTracker.Entries()) {
if (!EntitiesToExclude().Contains(entry.Entity.ToString())) {
switch (entry.State) {
case EntityState.Added:
entry.CurrentValues["IsDeleted"] = false;
break;
case EntityState.Deleted:
entry.State = EntityState.Modified;
entry.CurrentValues["IsDeleted"] = true;
foreach(var navigationEntry in entry.Navigations.Where(n = >!n.Metadata.IsDependentToPrincipal())) {
if (navigationEntry is CollectionEntry collectionEntry) {
foreach(var dependentEntry in collectionEntry.CurrentValue) { (Entry(dependentEntry)).State = EntityState.Modified;
(Entry(dependentEntry)).CurrentValues["IsDeleted"] = true;
}
} else {
var dependentEntry = navigationEntry.CurrentValue;
if (dependentEntry != null) { (Entry(dependentEntry)).State = EntityState.Modified;
(Entry(dependentEntry)).CurrentValues["IsDeleted"] = true;
}
}
}
break;
}
}
}
}
我的解决方案是,
在映射过程中忽略集合,我删除了集合
AfterMap
.
我使用 AutoMapper.Collection
EqualityComparison
地图
CreateMap<SavePartnerRegistryResource, PartnerRegistry> ().EqualityComparison ((s, d) => s.PatientFileId == d.PatientFileId && s.PartnerFileId == d.PartnerFileId);
我将集合映射到一个单独的 List
,
List<PartnerRegistry> patientPartnersModelDataList = mapper.Map<List<SavePartnerRegistryResource>, List<PartnerRegistry>> (model.Partners.ToList (), patient.Partners.ToList ());
现在我可以得到 added/removed/modified 个实体如下,
IEnumerable<PartnerRegistry> partnerRegistryToRemoveList = patient.Partners.ToList ().Except (patientPartnersModelDataList);
IEnumerable<PartnerRegistry> partnerRegistryToAddList = patientPartnersModelDataList.Except (patient.Partners.ToList ());
IEnumerable<PartnerRegistry> partnerRegistryToUpdateList = patient.Partners.ToList ().Intersect (patientPartnersModelDataList.ToList ());
然后我 运行 foreach
遍历每个实体并使用上下文执行 Add/Remove 而不是修改 collection
,
为了更新,我从 partnerRegistryToUpdateList