Skip to content

Allow applications to create model building conventions (aka public conventions) #214

Description

@rowanmiller
  • Create public interfaces for metadata API to be used by conventions.
  • Change QueryFilter, DefiningQuery, IsEagerLoaded, IsShadowProperty , IsIndexedProperty, BeforeSaveBehavior AfterSaveBehavior to extension methods.
  • Throw when calling SetField would change the shadowness of a property or the name of the identifying MemberInfo.
  • Rename GetContainingPrimaryKey to FindContainingPrimaryKey.
  • Throw when calling SetField on a shadow or a field-only property.
  • Create public interfaces that the internal model builders implement, e.g. IConventionModelBuilder. This will allow cleaner API, without the explicit ConfigurationSource parameter.
  • Throw instead of silently failing when a non-explicit configuration call is invalid. This would require adding quiz API ('CanSet').
  • Make model element references consistent (e.g. Property vs IProperty vs string vs PropertyInfo)
  • Add provider-specific extension methods for convention metadata interfaces.
  • Rename and move RelationalAnnotations and RelationalAnnotationsBuilder to Core.
  • Remove unused methods on provider classes like SqlServerPropertyBuilderAnnotations.ColumnName.
  • Rename the Apply methods to be more descriptive and make the return types consistent, still allowing to stop the execution of the following conventions.
  • Pass CoreConventionSetBuilderDependencies to the constructor of all conventions.
  • Add convention type for configuring a type as owned and use it to remove ambiguous ownerships.
  • Track the model elements as they are modified by conventions, so a call to a convention never returns an element that is no longer in the model.
  • Add a way to delay the convention execution.
  • Move logic from ModelCustomizer to a convention.
  • Add SqlServerValueGenerationStrategy.None value that can be used to override the model default.
  • Filter out the core annotations changes from conventions.
  • Remove PropertyMetadataChanged() calls and don't cache indexes while the model is mutable.
  • Expose conventions as List on ConventionSet
  • Add methods to add/remove a convention to/from all appropriate convention lists.
  • Add a Conventions property of ConventionSetBuilder type to ModelConfigurationBuilder with methods to add/remove a convention

Activity

  1. added this to the 1.0.0 milestone on May 22, 2014
  2. changed the title [-]Model Definition: Provide API for customizing conventions[/-] [+]Model Definition: Provide API for custom conventions[/+] on Oct 9, 2014
  3. changed the title [-]Model Definition: Provide API for custom conventions[/-] [+]Model Definition: Custom conventions[/+] on Oct 9, 2014
  4. modified the milestones: Backlog, 7.0.0 on Nov 26, 2014
  5. sthiakos commented on Dec 26, 2015

    @sthiakos

    This was useful for me in the following Scenarios:

    • Undo default conventions applied by EF using DbModelBuilder
      ** Creating an IStoreModelConvention for AssociationType to remove underscore from FK names,
      ** Creating an IStoreModelConvention for EntitySet to remove underscore from m-2-m join tables/dbsets (not applicable right now to EF Core).
    • Dictate Entity Identity Behaviors - using the modelBuilder.Types() method:
      ** Auto-generation based on whether it's a Data-Dictionary/Lookup item (e.g. BlogType - static) vs an Entity (Blog - autogen). They are distinguished based on interface implementations.
    • Decouple code naming standards and DB naming standards - using the modelBuilder.Properties() method:
      ** Give a common Class property (e.g. "StateId") but an explicit db column name (e.g. "BlogStateId"). Same goes for "Id" in code but "{Type}Id" in database.

    Doing this once I was able to govern the entity definitions without requiring explicit entity declaration. I'm not advocating the same implementation, but do feel custom/override conventions are valuable.

    Cheers,

    Steve

  6. rowanmiller commented on Dec 28, 2015

    @rowanmiller
    ContributorAuthor

    Some of these types of things can be achieved by iterating over the model that is created by convention. This was not possible in EF6 because we collected a set of configuration during OnModelCreating and then used that plus conventions to build the model. But in EF Core we build an initial model from conventions and then pass that to OnModelCreating.

    Of course, this means you need to be mindful of ordering. For example, if a type wasn't included in the model by convention, then you need to make sure you add it before you iterate over the model and assign table names.

    Here is an example of appending an s to the table name.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      foreach (var entity in modelBuilder.Model.GetEntityTypes())
      {
        modelBuilder.Entity(entity.Name).ToTable(entity.Name + "s");
      }
    }
  7. sthiakos commented on Dec 29, 2015

    @sthiakos

    Thanks for the info Rowan,

    I'll just need to explicitly load my entities in this method for my domain assembly:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var entityTypes = myDomainAssembly.DefinedTypes
            .Where(ti => ti.IsClass && ti.IsPublic && !ti.IsAbstract && !ti.IsNested)
    
        foreach (var entityType in entityTypes)
        {
            modelBuilder.Entity(entityType).ToTable(entity.Name + "s");
        }
    }

    This makes me think that perhaps an OnEntityModelCreating(EntityTypeBuilder entityTypeBuilder) may help with conventions and keep things dynamic.

    Steve

  8. tibitoth commented on Jan 5, 2016

    @tibitoth

    I see that this issue is not a priority issue, but will the custom conventions be implemented before RC2, or just before RTM? (I hope it will be at least in RTM)

  9. ErikEJ commented on Jan 5, 2016

    @ErikEJ
    Contributor
  10. 120 remaining items

  11. removed their assignment
    on Aug 7, 2022
  12. added a commit that references this issue on Aug 7, 2022
    d152beb
  13. added a commit that references this issue on Aug 7, 2022
    6ec0da9
  14. modified the milestones: 7.0.0, on Aug 8, 2022
  15. changed the title [-]Expose model building conventions to applications[/-] [+]Allow applications to create model building conventions (aka public conventions)[/+] on Aug 18, 2022
  16. modified the milestones: , 7.0.0 on Nov 5, 2022
  17. vupham1111 commented on Nov 21, 2023

    @vupham1111

    hello, I come from this #27400
    does the ef core support the feature yet, if it does, I hope you can feed me some guidance, thank you so muchhhh

  18. ajcvickers commented on Nov 21, 2023

    @ajcvickers
    Contributor
  19. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions