简体   繁体   English

具有软删除功能的 EntityFramework OwnedType

[英]EntityFramework OwnedType with Soft-delete

I have an issue with Owned Types on my entites, because I added an override for SaveChanges to make soft-deletes possible.我的实体上的自有类型存在问题,因为我为 SaveChanges 添加了覆盖以使软删除成为可能。 Basically I override the state of the Entity to modified instead of delete in the SaveChanges and add some meta information to the entity.基本上,我在 SaveChanges 中覆盖实体的 state 以修改而不是删除,并向实体添加一些元信息。 This works well unless I add a OwnedType onto the entity.除非我将 OwnedType 添加到实体上,否则这很有效。

When I do this, EF tries to set all values from the OwnedType to NULL, which is not possible in my example because the Strings are not nullable.当我这样做时,EF 会尝试将 OwnedType 中的所有值设置为 NULL,这在我的示例中是不可能的,因为字符串不可为空。

I am not exactly sure how I can fix this behaviour, because I need to set the DeleteBehaviour to NoAction, but I couldn't find a way to do this at the moment.我不确定如何解决此问题,因为我需要将 DeleteBehaviour 设置为 NoAction,但目前我找不到执行此操作的方法。 Can anyone explain to me how I can solve this issue?谁能向我解释如何解决这个问题?

MainEntity.cs MainEntity.cs

public class MainEntity: SoftDeleteEntityBase
{
    public int Id { get; set; }
    public LocalizedStringSet MultilangTitle { get; set; } = new LocalizedStringSet();
}

LocalizedStringSet.cs本地化字符串集.cs

public class LocalizedStringSet
{
    [MaxLength(4000)]
    public string En { get; set; } = string.Empty;

    [MaxLength(4000)]
    public string Fr { get; set; } = string.Empty;
}

MainEntityConfiguration.cs MainEntityConfiguration.cs

public class MainEntityConfiguration : IEntityTypeConfiguration<MainEntity>
{
    public void Configure(EntityTypeBuilder<MainEntity> builder)
    {
        if (builder is null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id).ValueGeneratedOnAdd();
        builder.OwnsOne(o => o.MultilangTitle);
    }
}

ApplicationDbContext.cs ApplicationDbContext.cs

public override int SaveChanges()
{
    if (entry.Entity is ISoftDeleteEntityBase managedCreationSoftDelete)
        {
            switch (entry.State)
            {
                case EntityState.Deleted:
                {
                        entry.State = EntityState.Modified;
                        managedCreationSoftDelete.DeletedBy = creator;
                        managedCreationSoftDelete.Deleted = DateTime.Now;
                        break;
                }

            }
        }
    return base.SaveChanges();
}

I found a way to solve it, but I am not sure if there is a better way to do this.我找到了解决它的方法,但我不确定是否有更好的方法来做到这一点。

public override int SaveChanges()
{
    if (entry.Entity is ISoftDeleteEntityBase managedCreationSoftDelete)
        {
            switch (entry.State)
            {
                case EntityState.Deleted:
                {
                        entry.State = EntityState.Modified;
                        managedCreationSoftDelete.DeletedBy = creator;
                        managedCreationSoftDelete.Deleted = DateTime.Now;
                        
                        foreach (var reference in entry.References)
                        {
                            if (reference.TargetEntry?.Entity.GetType() == typeof(LocalizedStringSet))
                            {
                                if (reference.TargetEntry != null)
                                {
                                    reference.TargetEntry.State = EntityState.Unchanged;
                                }
                            }
                        }

                        break;
                }

            }
        }
    return base.SaveChanges();
}


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM