EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

Z.EntityFramework.Plus.EF6: unwanted Id

Open asienicki opened this issue 8 years ago • 5 comments

Hi,

I just tested your library and I found 1 unwanted behavior.

I'm using attributes AuditInclude over class and AuditExclude over property. In case when I change only property with AuditExclude attribute - to table in database AuditEntryProperties only Id is saved.

I read about this in issue #8 but it's not solved problem. When you change ignored property of course that your entity state is modified.

When audit is empty - saving only Id is like empty useless record.

Of course I enabled data anottations:

AuditManager.DefaultConfiguration.ExcludeDataAnnotation();

My class look like this:

[AuditInclude]
    public class Response : BaseEntity
    {
        [AuditExclude]
        public int OccurenceCount { get; set; }

        public Message Message { get; set; }
}

I think that I can have some fun with this:

AuditManager.DefaultConfiguration.AutoSavePreAction = (context, audit) =>
      (context as UkuzimelaDatabaseContext)?.AuditEntries.AddRange(audit.Entries);

and checking if you trying save to database only Id, but it's not the best solution.

asienicki avatar Feb 18 '17 14:02 asienicki

Hello @asienicki ,

I'm not sure I understand what you are trying to say. It's still early here, so maybe that's the reason ;)

I have created a test with your scenario which excludes the OrccurenceCount: Form_Request_Audit_DataAnnotation.txt

Let me know what I'm missing.

Best Regards,

Jonathan

JonathanMagnan avatar Feb 18 '17 14:02 JonathanMagnan

Hi,

Your test scenario is almost ok.

But my case looks like this:

            using (var ctx = new EntityContext())
            {
                var audit = new Audit();

                var firstMessage = ctx.Responses.First(); 
                firstMessage.OccurenceCount = 2;

                ctx.Responses.Add(new Response {Message = "z3", OccurenceCount = 3});

                ctx.SaveChanges(audit);
            }

I removed line: firstMessage.Message = "z2"; Soo... in this case OccurenceCount was changed - but this property is ignored and in fact shouldn't be trigger as change for auditing.

In this scenario audit will register in database only Id changed Response.

I hope that I explained clearly my issue - despite the bad grammar :)

Regards, Artur

asienicki avatar Feb 20 '17 12:02 asienicki

Hello @asienicki ,

Thank you, I understand now your point.

By default, the current behavior will not be modified since I believe even if only the PK keys is audited, it also provide you the information of the Date the changes has occurred.

However, an option could be added for your requirement. Honestly, the hardest part of this request is finding the option name ;)

AuditManager.[NameToBeDetermined]= true;

Best Regards,

Jonathan

JonathanMagnan avatar Feb 20 '17 15:02 JonathanMagnan

Yes :)

Maybe something like this: "AlwaysRegisterChangedPublicKey" "AllowSaveOnlyId"

asienicki avatar Feb 20 '17 16:02 asienicki

Any updates on this?

I have same issue and can't find any solution for now...

tahq69 avatar May 27 '22 09:05 tahq69