win7user10 / Laraue.EfCoreTriggers

Library to write triggers in C# with EF.Core
MIT License
112 stars 20 forks source link

Support for owned entities #54

Open Xriuk opened 2 years ago

Xriuk commented 2 years ago

I think this awesome library could support collection of owned entities, which get mapped to separate tables. But I guess this would require some effort, because as of now all the triggers reference only generic types, and an owned type could have many underlying EF types, since it could be used in multiple entities.

I'm willing to help if you could guide me in what needs to be done in order to account for this. I think that adding a reference to an EF entity would be enough, from there it could pick all the informations about the table, while all the generic methods would stay the same, since the columns are all the same.

win7user10 commented 2 years ago

Hi, I think for each Action can be added an opportunity to pass a specific type for each trigger. It can have the next syntax

modelBuilder.Entity<Transaction>()
    .AfterUpdate(Action, typeof(DerivedTransaction))

This will require changes in the class EntityTypeBuilderExtensions to support passing the type, passing this parameter to each trigger builder (namespace Laraue.EfCoreTriggers.Common.TriggerBuilders), e.g. for class OnDeleteTriggerDeleteAction and also modifying classes derived from BaseTriggerVisitor to use passed type if it specified otherwise type from the expression.

Xriuk commented 2 years ago

I'm not quite sure about the differences from EF and EF Core, in the latter the type of the entities in the model is IEntityType. So this would require an additional property in ITrigger, and then the modifications you said, right?

win7user10 commented 2 years ago

EF Core allows to retrieve information about any entity by its CLR type. ITrigger already has a property TriggerEntityType. This property in the base implementation Trigger<TTriggerEntity> sets equal to the generic type of a trigger. All meta information (table name, column name etc) is extracting from this type. Constructor of this class could be extended to take the real type of the trigger (with the check that this type is derived from the generic type).

Xriuk commented 2 years ago

The problem is that owned entities are not mapped to a CLR type, for example an owned Category could belong to a Shop or a Blog, and thus being mapped to two different tables, while being a single CLR type. So instead of using Type to create triggers maybe using IEntityType would be better because it would account for all the EF Core types, even many-to-many relationships mapped to a separate table, as well as owned entities.