Base solution for your next web application
Open Closed

Using FullAuditedEntity on 2nd (non-asp.net Zero) DB #9307


User avatar
0
HCCNOV created

Is there an example of how to provision the Audited Entity model for another db that is not the main asp.net Zero db? I've followed the instructions which show how to set up the tracked entities in the "Default" db within the {YourProjectName}EntityFrameworkCoreModule.cs

I've set the class up properly using [Audited] and FullAuditedEntity.

This of course will not track audit class because this class belongs to the 2nd dbContext (not ABP/Asp.Net Zero)

Do I need to create the "{YourProjectName}EntityFrameworkCoreModule.cs" type file for the other DB and if so, how?

Thank you

Here is my {YourProjectName}EntityFrameworkCoreModule.cs

 public override void PreInitialize()
        {
            if (!SkipDbContextRegistration)
            {
                //SAR
                Configuration.ReplaceService<IConnectionStringResolver, MyConnectionStringResolver>();

                Configuration.Modules.AbpEfCore().AddDbContext<NOVZERODbContext>(options =>
                {
                    if (options.ExistingConnection != null)
                    {
                        NOVZERODbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                    }
                    else
                    {
                        NOVZERODbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                    }
                });

                // Configure second DbContext
                Configuration.Modules.AbpEfCore().AddDbContext<Data70DbContext>(options =>
                {
                    if (options.ExistingConnection != null)
                    {
                        Data70DbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection);
                    }
                    else
                    {
                        Data70DbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString);
                    }
                });
            }

            // Set this setting to true for enabling entity history.
            Configuration.EntityHistory.IsEnabled = true;

            // Uncomment below line to write change logs for the entities below:
             Configuration.EntityHistory.Selectors.Add("NOVZEROEntities", EntityHistoryHelper.TrackedTypes);
             Configuration.CustomConfigProviders.Add(new EntityHistoryConfigProvider(Configuration));
        }

Here are my TrackedTypes (CustomersAndAcccount.Property is in the 2nd dbContext)

  public static readonly Type[] HostSideTrackedTypes =
        {
            typeof(OrganizationUnit),
            typeof(Role),
            typeof(Tenant)
        };

        public static readonly Type[] TenantSideTrackedTypes =
        {
            typeof(CustomersAndAccounts.Property),
            typeof(OrganizationUnit),
            typeof(Role)
        };

        public static readonly Type[] TrackedTypes =
            HostSideTrackedTypes
                .Concat(TenantSideTrackedTypes)
                .GroupBy(type => type.FullName)
                .Select(types => types.First())
                .ToArray();

And the Model I wish to track:

namespace NOVZERO.CustomersAndAccounts
{
    [Table("tblPropertyInformation")]
    [Audited]
    public class Property : Entity
    {

        [Key]
        public override int Id { get; set; }

I was also wondering, for the Change Tracking, does the class need to have "FullAuditedEntity" or "AuditedEntity" on it or will the [Audited] Property do the job?


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @HCCNOV,

    Is your second DbContext inherits from AbpZeroDbContext or AbpZeroCommonDbContext ?

    I was also wondering, for the Change Tracking, does the class need to have "FullAuditedEntity" or "AuditedEntity" on it or will the [Audited] Property do the job?

    For the latest versions (5.x I guess), Audited attribute should be enough.

  • User Avatar
    0
    HCCNOV created

    Hello.

    It inherits from AbpDbContext.

    I changed our second DbContext to AbpZeroDbContext and added all the ABP/Zero tables to that DB and I have the Auditing working. I don't understand this paradigm as I'm not sure why I would have 2 AbpLanguageTexts in my application (one for each db)

    Does that seem right? (our second DB is a very old SQL database and knows nothing about Zero as it was originally connected to an AccessApp and now connected to Zero)

    Using version 8.x MVC JQuery.

    Also, I have seen this post: StackOVerflow from last year: ASP.NET Boilerplate multiple databases and DbContexts

    namespace NOVZERO.EntityFrameworkCore
    {
        public class Data70DbContext : AbpDbContext
        {
            public Data70DbContext(DbContextOptions<Data70DbContext> options)
               : base(options)
            {
            }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @HCCNOV,

    Did that work in that way ?

    Thanks,

  • User Avatar
    0
    HCCNOV created

    The Audit feature DOES work but I have to have the ABP/Zero tables in the older database AS WELL AS the Asp.Net Zero db. Is it possible to have one database (the original older SQL database) and not create the Asp.Net Zero database with all the ABP/Zero tables? Is that the pattern I should have followed?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If I understand it correctly, you can create your own implementation of https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Zero.Common/Auditing/AuditingStore.cs.

    For example, you can create MyAuditingStore and replace default implementation like;

    Configuration.ReplaceService<IAuditingStore, MyAuditingStore>(DependencyLifeStyle.Transient);

    in the PreInitialize method of your EF Core module. But, your implementation will be used for saving all audit logs if that's what you want.