zzzprojects / EntityFramework-Plus

Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more
https://entityframework-plus.net/
MIT License
2.26k stars 318 forks source link

Audit: resolving FK-Property Dislayname and relate Parent/Child #65

Open eismue opened 8 years ago

eismue commented 8 years ago

Hello and good day,

recently I found your audit solution and I'm virtual attired. But these points bothers me and must be asked:

Thanks for an answer and of course your work.

zzzprojects commented 8 years ago

Hello @eismue ,

is there a way to resolve an userfriendly name from a foreign key property instead of writing numeric foreign keys value in old/newvalue?

I'm not sure to understand this question, can you give me a small example of the actual result and wished result.

when is dbo.AuditProperties.RelationName filled

It's filled for EF5 && EF6 when a new relation is added or removed. If you are using EF Core, it's still not used but could be used in the future.

there seems no way to easily query a changes made on a user-object and its related detail-data

You are right, that's a great suggestion. We will make some research about this suggestion but I don't think this could be implemented before version 2.0.

Please let me know more about your first question and if you are using EF Core, we will improve our comment according to your answers.

eismue commented 7 years ago

Hello and thanks for the quick answer and sorry for the late reply...

Point 1: ... is there a way to resolve an userfriendly name from a foreign key property instead of writing numeric foreign keys value in old/newvalue?... I'm not sure to understand this question, can you give me a small example of the actual result and wished result. .... Lets say there is a users-table (Id, Lastname, Firstname,) and an m:n relation for language skills (languages-table: Id, LanguageName,...). One user can have many language skills. The table to map between users and languages is not surprisingly shaped like Id, UserId, LanguageId. Problem: only the keys will be auited (UserId, LanguageId). What I need: insteed of the pure scalar int/bigint-value I want for languages the value from the languages-table LanguageName-field. This is important, because and enduser without any database skills must see what has been changed. Resolving the language-name at query-time is to expensive and cannot work, if an entity has been deleted. I hope I could clearify my suit.

Point 2: ... when is dbo.AuditProperties.RelationName filled ... We are using EF6. Sorry for not hinting at this. And in our first testing scenarios we only were updating. So I understand why this field was empty.

Thank you.

zzzprojects commented 7 years ago

Hello @eismue ,

We implemented in the v1.4.10 an AuditDisplay DataAnnotation.

You can see more how to implement here: Issue #67

Let me know if this solution or a similar solution is what you are looking for.

My guess is this solution doesn't fully fill your requirement since you will need to set the DisplayName at runtime (the name change with language value by example).

Best Regards,

Jonathan

eismue commented 7 years ago

Hi and thanks. The AuditDisplay/DataAnnotation unfortunately cannot help at this. Thats because Displayname can only change the description, but not the saved value that must be dynamically evaluated from a navigation-property, if its an foreign key that changed. And: we are using edmx-models, so data-annotations are not applicable :)

However, have a nice day.

krajek commented 6 years ago

Hi, I also have the same problem and haven't yet found any complete solution. Simplified code:

class Machine { int FamilyId {get; set} }
class MachineFamily { int FamilyId {get; set} string Name {get; set} }

When I change the FamilyId field of Machine object the audit contains changes from id to id (for example from 1 to 5). I would like it to display OldValue= "SomeNameOfFamily1", NewValue="SomeOtherNameOfFamily5".

I tried two approaches: A) Specifying AuditEntryPropertyFactory, but even if I explicitly set NewValue it was not used B) Specifying formatter in EntityValueFormatters. This solution is very close, but in the end, formatter receives only the value of the property and not the entity.

Could you point me to some other idea? Did anybody implement such feature successfully?