Skip to content

Conversation

@hikalkan
Copy link
Member

@hikalkan hikalkan commented Aug 18, 2025

Allow developers to add their custom conventional mapping logic or customize existing logic when they call the ConfigureByConvention method.

Example:

builder.Entity<Customer>(b =>
{
    b.ToTable("Customers"); 

    b.ConfigureByConvention(); // Auto configure by conventions

    b.Property(x => x.Name).IsRequired().HasMaxLength(128);
    b.Property(x => x.EmailAddress).IsRequired(false).HasMaxLength(256);
});

When we configure our entity like that, the b.ConfigureByConvention() method configures ABP's base properties, like SoftDelete, CreationTime, TenantId, ...

See the AbpEntityTypeBuilderExtensions class for all.

To add your own convention, add a new item to the static AbpEntityTypeBuilderExtensions.ConventionalConfigurers list. Example:

AbpEntityTypeBuilderExtensions.ConventionalConfigurers
    .Add(new NamedEntityConfigurer("MyEmailAddressConfigurer", entity =>
    {
        if (entity.Metadata.ClrType.IsAssignableTo<IHasEmailAddress>())
        {
            entity
                .Property("EmailAddress")
                .HasMaxLength(256);
        }
    }));

This example checks if the entity implements IHasEmailAddress (which defines the EmailAddress property), then it sets max length for that property.

You should write this configuration before you first use the DbContext object, typically in the PreConfigureServices method of your module class, or in the Program.cs, before you initialize your application.

You can also remove or replace ABP's default configurers using the AbpEntityTypeBuilderExtensions.ConventionalConfigurers list.

@maliming maliming merged commit 4feaca4 into dev Aug 19, 2025
3 checks passed
@maliming maliming deleted the efcore-custom-conventional-configurer branch August 19, 2025 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants