Base solution for your next web application
Open Closed

EntityHistory working only partially when using custom repository #7866


User avatar
0
enio created

In order to update an entity object including child object properties we have created a custom repository and are using the Update-Method directly on the EF-Context. But when we update the entities like this we don't have any entries in the AbpEntityPropertyChanges table. However we do have a new entry in AbpEntityChanges. Is there a simple way to fix this issue or is is meanwhile fixed - we are on version 6.7 from last february.

Do we have to implement IEntityHistoryStore in our custom repository - if yes could you please provide a simple example?


6 Answer(s)
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi @enio, when you save the entity via Ef Context, does this context inheirit from Abp Db Context?

    if you see only entity change being created and not property changes, it means that there is no property changes for your entity but it is audited. is that correct?

  • User Avatar
    0
    enio created

    The context I am using is comming from the base class EfCoreRepositoryBase and provided to this base class in the base call of my constructor. My constructor gets the context injected:

    IDbContextProvider<web_managerDbContext> dbContextProvider

    Yes the change is audited. The concerning entity class has the [Audited]-annotation on class level and the class is listed in EntityHistoryHelper. When I update the entity with the default abp IRepository<myEntity> everything is working just fine. But as soon as I update the entity with the context from my custom repository the changed entities are not detected / not listed in the AbpEntityPropertyChanges table.

  • User Avatar
    1
    ryancyq created
    Support Team

    Can you share the code for the custom repository and web_managerDbContext (relevant to the entity with audited attribute)?

  • User Avatar
    0
    enio created

    This is the Context: https://download.enio.ch/PEPDVPXM/web_managerDbContext.cs ..it's actually the original AspNetZero context

    This is our repository:

    public class InvoiceRIrTaskRepository : web_managerRepositoryBase<IrTask>, IInvoiceRIrTaskRepository
    {
        public InvoiceRIrTaskRepository(IDbContextProvider<web_managerDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }
    
        public async Task UpdateIrTaskGraph(IrTask task)
        {
            task.IrCase.Documents = new List<Document>();
    
            Context.Update(task);
            Context.SaveChanges();
        }
    }
    

    This is our repositories interface:

    public interface IInvoiceRIrTaskRepository : IRepository<IrTask>
    {
        Task UpdateIrTaskGraph(IrTask task);
    }
    

    And this is the base repository - actually the original file as we found it in AspNetZero 6.7

        /// <summary>
        /// Base class for custom repositories of the application.
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <typeparam name="TPrimaryKey">Primary key type of the entity</typeparam>
        public abstract class web_managerRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<web_managerDbContext, TEntity, TPrimaryKey>
            where TEntity : class, IEntity<TPrimaryKey>
        {
            protected web_managerRepositoryBase(IDbContextProvider<web_managerDbContext> dbContextProvider)
                : base(dbContextProvider)
            {
    
            }
    
            //add your common methods for all repositories
        }
    
        /// <summary>
        /// Base class for custom repositories of the application.
        /// This is a shortcut of <see cref="web_managerRepositoryBase{TEntity,TPrimaryKey}"/> for <see cref="int"/> primary key.
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        public abstract class web_managerRepositoryBase<TEntity> : web_managerRepositoryBase<TEntity, int>
            where TEntity : class, IEntity<int>
        {
            protected web_managerRepositoryBase(IDbContextProvider<web_managerDbContext> dbContextProvider)
                : base(dbContextProvider)
            {
    
            }
    
            //do not add any method here, add to the class above (since this inherits it)!!!
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @enio

    Is this problem solved for you ? If not, Could you share your Entity definition and a sample update information to reproduce this problem ?

    Thanks,

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because it has not had recent activity for a long time.