Laraue.EfCoreTriggers icon indicating copy to clipboard operation
Laraue.EfCoreTriggers copied to clipboard

Support for owned entities

Open Xriuk opened this issue 1 year ago • 4 comments

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.

Xriuk avatar Sep 26 '22 08:09 Xriuk

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.

win7user10 avatar Sep 26 '22 20:09 win7user10

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?

Xriuk avatar Sep 26 '22 20:09 Xriuk

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).

win7user10 avatar Sep 27 '22 19:09 win7user10

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.

Xriuk avatar Sep 27 '22 20:09 Xriuk