C#C
C#2y ago
Rillian

Help with EF Core polymorphic associations

Hi everyone,

I'm currently at a IT job in a medical organisation. Through working with various software packages at scale I noticed two things.
One was that the ability to add small amounts of plain text to various bits of data is very useful in unusual situations and quickly leads to the emergence of new workflows.
And the second was that audit logs are invaluable.

As I'm slowly getting to grips with EF Core (mainly used with ASP.NET) I've been trying to use polymorphic associations as a "silver bullet" for those kinds of things.
The idea is that I have many different tables (users, tasks, requests, etc.) that can all have associated audit logs and comment sections. The tables containing the audit log events and comments would have two additional fields containing the type and primary key of the item they're associated with. E.g. one comment may be associated with ("request", 34) or a user locks themselves out and an entry is saved in the audit log referencing ("user", 21).

As far as I understand my entities should include Discriminator fields but I haven't been able to find the information required to make it work. All relevant information I find seems to be based around an inheritance situation e.g. User and Contact inherit from Person instead of my use case with essentially replicates a regular many to one relationship.

I'd also like it to support multiple inheritance so, for example, Page could inherit from AuditLog and Comments.

Any advice on how to approach this would be much appreciated
Thanks in advance
Was this page helpful?