Реализация мягкого удаления Entity Framework с использованием перехватчика базы данных не работает

я реализовал мягкое удаление базы данных (логический флаг, который помечает записи как удаленные), используя следующий учебник: http://www.codeguru.com/csharp/csharp/soft-deleting-entities-cleanly-using-entity-framework-6-interceptors.html

мне кажется очень хорошей реализацией, потому что после настройки мягкого удаления применяется к модели просто добавив [SoftDelete("IsDeleted")] Примечание. Проблема в том, что пока это не работает.

источник надежные, и они даже опубликовали пример своего решения:https://github.com/rakeshbabuparuchuri/EFExpensionPoints

можете ли вы взглянуть на мой код, если я сделал что-то неправильно при применении мягкого удаления к моему проекту?

это Модель:

[SoftDelete("IsDeleted")]
public class BC_Instance
{
    public int ID { get; set; }
    public bool IsDeleted { get; set; }
}

ApplicationDbContext.cs:

namespace bcplatform2.Models
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        // Add a DbSet for each one of your Entities
        //public DbSet<VirtualGuest> VirtualGuests { get; set; }
        public DbSet<BC_Instance> BiocloudInstances { get; set; }

        static ApplicationDbContext()
        {
            Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        protected new void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var conv = new AttributeToTableAnnotationConvention<SoftDeleteAttribute, string>(
               "SoftDeleteColumnName",
               (type, attributes) => attributes.Single().ColumnName);

            modelBuilder.Conventions.Add(conv);
        }
    }
}

ApplicationDbConfiguration.cs

namespace bcplatform2.DAL
{
    public class ApplicationDbConfiguration : DbConfiguration
    {
        public ApplicationDbConfiguration()
        {
            AddInterceptor(new SoftDeleteInterceptor());
        }
    }
}

SoftDeleteAttribute.cs:

namespace bcplatform2.Helpers
{
    public class SoftDeleteAttribute : Attribute
    {
        public SoftDeleteAttribute(string column)
        {
            ColumnName = column;
        }

        public string ColumnName { get; set; }

        public static string GetSoftDeleteColumnName(EdmType type)
        {
            MetadataProperty annotation = type.MetadataProperties
                .Where(p => p.Name.EndsWith("customannotation:SoftDeleteColumnName"))
                .SingleOrDefault();

            return annotation == null ? null : (string)annotation.Value;
        }
    }
}

SoftDeleteInterceptor.cs

я заметил, что SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType) не находит атрибут мягкого удаления и возвращает null. Но я не знаю, почему.

namespace bcplatform2.Helpers
{
    public class SoftDeleteInterceptor : IDbCommandTreeInterceptor
    {
        public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
        {
            if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace)
            {
                var queryCommand = interceptionContext.Result as DbQueryCommandTree;
                if (queryCommand != null)
                {
                    var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor());
                    interceptionContext.Result = new DbQueryCommandTree(
                        queryCommand.MetadataWorkspace,
                        queryCommand.DataSpace,
                        newQuery);
                }

                var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree;
                if (deleteCommand != null)
                {
                    var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType);
                    if (column != null)
                    {
                        var setClauses = new List<DbModificationClause>();
                        var table = (EntityType)deleteCommand.Target.VariableType.EdmType;
                        if (table.Properties.Any(p => p.Name == column))
                        {
                            setClauses.Add(DbExpressionBuilder.SetClause(
                                    DbExpressionBuilder.Property(
                                        DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName),
                                        column),
                                    DbExpression.FromBoolean(true)));
                        }

                        var update = new DbUpdateCommandTree(
                            deleteCommand.MetadataWorkspace,
                            deleteCommand.DataSpace,
                            deleteCommand.Target,
                            deleteCommand.Predicate,
                            setClauses.AsReadOnly(),
                            null);

                        interceptionContext.Result = update;
                    }
                }
            }
        }
    }
}

IdentityConfig.cs

public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        InitializeIdentityForEF(context);
        base.Seed(context);
    }

    //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
    public static void InitializeIdentityForEF(ApplicationDbContext db)
    {
        //Initialize users and roles...
    }
}

2 ответов


в ApplicationDbContext есть ошибка.cs:

protected new void OnModelCreating(DbModelBuilder modelBuilder) {...}

вы используете " new "вместо " override", поэтому OnModelCreating никогда не выполняется (попробуйте добавить точку останова, чтобы проверить ее). Так AttributeToTableAnnotationConvention никогда не работает и сущности аннотации не добавил.

изменение его

protected override void OnModelCreating(DbModelBuilder modelBuilder) {...}

все получится


Ну, ваш код кажется мне прекрасным. Возможно, есть небольшая ошибка, которая нарушает ваше приложение. Вы можете попробовать это:

  1. удалить SoftDeleteAttribute С BC_Instance

  2. редактировать OnModelCreating метод

    AttributeToTableAnnotationConvention<SoftDeleteAttribute, string> conv =
       new AttributeToTableAnnotationConvention<SoftDeleteAttribute, string>(
          "SoftDeleteColumnName",
          (type, attributes) => attributes.Single().ColumnName);
    
    modelBuilder.Conventions.Add(conv);
    //this will dynamically add the attribute to all models
    modelBuilder.Types().Configure(delegate(ConventionTypeConfiguration i)
    {
        i.HasTableAnnotation("SoftDeleteColumnName", Entity.G etSoftDeleteColumnName());
    });
    
  3. удалить ApplicationDbConfiguration класс

  4. редактировать конструктор контекста

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        DbInterception.Add(new SoftDeleteInterceptor());
    }
    

надеюсь, что это помогает!