I want to add ShadowProperts to all entities.
What I did is to add shadowProperties to the parent class (Entity).
For this purpose I have written the following code in DbContext:
var entityTypes = builder.Model.GetEntityTypes()
.Where(e => typeof(Entity).IsAssignableFrom(e.ClrType) && e.ClrType != typeof(Entity));
foreach (var entityType in entityTypes)
{
builder.Entity(entityType.ClrType)
.Property<DateTime>("CreatedDate")
.HasDefaultValueSql("GETUTCDATE()");
builder.Entity(entityType.ClrType)
.Property<DateTime?>("UpdatedDate")
.HasConversion(new ValueConverter<DateTime, DateTime>(
v => v.ToUniversalTime(),
v => DateTime.SpecifyKind(v, DateTimeKind.Utc)))
.ValueGeneratedOnUpdate();
}
But the problem is that it affects entities that are OwnsOne or OwnsMany
Unable to create a ‘DbContext’ of type ‘ContentCommandDbContext’. The
exception ‘The entity type ‘Comment’ cannot be configured as non-owned
because it has already been configured as a owned. Use the nested
builder inOwnsOne
orOwnsMany
on the owner entity type builder to
further configure this type. If you want to override previous
configuration first remove the entity type from the model by calling
‘Ignore’. See https://aka.ms/efcore-docs-owned for more information
and examples.’ was thrown while attempting to create an instance. For
the different patterns supported at design time, see
https://go.microsoft.com/fwlink/?linkid=851728
To avoid this error, I need to identify the entities that are OwnsOne or OwnsMany and not put them in this code.I can do this using hardcode, for example:
var entityTypes = builder.Model.GetEntityTypes()
.Where(e => typeof(Entity).IsAssignableFrom(e.ClrType) && e.ClrType != typeof(Entity) && e.ClrType != typeof(Comment));
And it works fine, but it becomes problematic during development (we all know). Can this be done using reflection? I am new to reflection.