Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
23 tasks done
Tracked by #24106 ...
rowanmiller opened this issue May 22, 2014 · 26 comments · Fixed by #28613
Closed
23 tasks done
Tracked by #24106 ...
Labels
area-conventions area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. ef6-parity punted-for-6.0 type-enhancement
Milestone

Comments

@rowanmiller
Copy link
Contributor

rowanmiller commented May 22, 2014

  • 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
@rowanmiller rowanmiller added this to the 1.0.0 milestone May 22, 2014
@rowanmiller rowanmiller changed the title Model Definition: Provide API for customizing conventions Model Definition: Provide API for custom conventions Oct 9, 2014
@rowanmiller rowanmiller changed the title Model Definition: Provide API for custom conventions Model Definition: Custom conventions Oct 9, 2014
@rowanmiller rowanmiller removed the pri0 label Nov 26, 2014
@rowanmiller rowanmiller modified the milestones: Backlog, 7.0.0 Nov 26, 2014
@sthiakos
Copy link

sthiakos commented Dec 26, 2015

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

@rowanmiller
Copy link
Contributor Author

rowanmiller commented Dec 28, 2015

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");
  }
}

@sthiakos
Copy link

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

@tibitoth
Copy link

tibitoth commented Jan 5, 2016

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)

@ErikEJ
Copy link
Contributor

ErikEJ commented Jan 5, 2016

According to the current roadmap it will be in 7.0.2 (?) https://github.com/aspnet/EntityFramework/wiki/Roadmap

@tibitoth
Copy link

tibitoth commented Jan 5, 2016

Ohh I missed this. Thank you.

@ajcvickers
Copy link
Member

Consider a way to get from IMutableEntityType (or similar) to the InternalEntityTypeBuilder (or similar) so that changes can be flagged as "by convention" when making changes to an IMutableEntityType. See comments on PR #4688.

@AndriySvyryd AndriySvyryd self-assigned this Apr 1, 2016
AndriySvyryd added a commit that referenced this issue Mar 1, 2017
…ns and tracking objects modified by conventions.

  When set on ConventionDispatcher ConventionScope captures the conventions to be executed.
  ConventionBatch sets a ConventionScope and executed the stored conventions when it's run or disposed.
  When conventions captured in a ConventionScope are executed any triggered conventions are captured in a new ConventionScope to avoid keeping all the convention chain on the stack.
  This changes the order of convention execution which exposed some issues in code and tests
Allow specifying the dependend end without specifying the FK properties,
Throw when the same property is specified several times when adding a key, foreign key or indexes.
Remove Mock usage from ConventionDispatcherTest

Part of #214
@AraHaan

This comment was marked as off-topic.

AndriySvyryd added a commit that referenced this issue Aug 5, 2022
AndriySvyryd added a commit that referenced this issue Aug 5, 2022
AndriySvyryd added a commit that referenced this issue Aug 5, 2022
ghost pushed a commit that referenced this issue Aug 5, 2022
AndriySvyryd added a commit that referenced this issue Aug 5, 2022
AndriySvyryd added a commit that referenced this issue Aug 5, 2022
AndriySvyryd added a commit that referenced this issue Aug 6, 2022
AndriySvyryd added a commit that referenced this issue Aug 6, 2022
AndriySvyryd added a commit that referenced this issue Aug 6, 2022
AndriySvyryd added a commit that referenced this issue Aug 7, 2022
@AndriySvyryd AndriySvyryd removed their assignment Aug 7, 2022
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 7, 2022
AndriySvyryd added a commit that referenced this issue Aug 7, 2022
AndriySvyryd added a commit that referenced this issue Aug 7, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc1 Aug 8, 2022
@ajcvickers ajcvickers changed the title Expose model building conventions to applications Allow applications to create model building conventions (aka public conventions) Aug 18, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc1, 7.0.0 Nov 5, 2022
@vupham1111
Copy link

vupham1111 commented Nov 21, 2023

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

@ajcvickers
Copy link
Member

@vupham1111 See https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#model-building-conventions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-conventions area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. ef6-parity punted-for-6.0 type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.