Skip to content

Pre-model convention configuration overrides foreign key data type #3756

Closed

Description

@wdhenrik

I've used pre-model configuration to default all my strings to a max length of 500 as I don't want MAX columns used everywhere.

HOWEVER, when I create a navigation property, the pre-model convention overrides the data of the foreign key relationship. For example, my primary key is a varchar(10). When referenced as a foreign key, it should also be a varchar(10), but the pre-model convention is applied instead, creating a varchar(500).

A foreign key column should match the primary key definition exactly. With the current implementation, using pre-model convention requires the foreign key column and type to be explicitly declared, requiring additional code, unnecessarily duplicating the defintion, and introducing a possible point of failure if they are not kept in synch.

I would prefer that foreign keys are excluded from pre-model configuration. If that is not an option, I would like to see an additional flag added to the configuration builder to prioritize the key data type in a relationship

configurationBuilder
    .Properties<string>()
    .HaveMaxLength(500)
    .AreUnicode(false)
//suggestion
    .PreferTypeFromKey();

Activity

  1. ajcvickers commented on Mar 7, 2022

    @ajcvickers
    Contributor

    Notes from triage: We should update the docs to make this clear. However, because pre-convention runs before conventions or regular model building, it is not feasible to predicate these behaviors on information that is not inherently part of the CLR type.

  2. transferred this issue fromdotnet/efcoreon Mar 7, 2022
  3. added this to the Backlog milestone on Mar 7, 2022
  4. wdhenrik commented on Mar 8, 2022

    @wdhenrik
    Author

    Can I beg that this gets more than just a documentation update?

    Pre-model convention configuration will be of very limited use if it overrides the foreign key definition. It silently requires a choice between configuring all non-key columns of T or all foreign-key relationships of T. With this behavior, it adds development complexity and maintenance overhead, and the PK/FK type mismatch error doesn't occur until the migration is attempted.

    Strings are defaulted to nvarchar(max), but that is overridden for generated foreign keys. Why can't the pre-model configuration change the default at that level since the desired FK behavior already exists?

  5. AndriySvyryd commented on Apr 20, 2022

    @AndriySvyryd
    Member

    @wdhenrik Pre-convention model configuration is really a blunt way to apply simple configuration in bulk. If you need any subtlety or conditional logic then custom conventions (dotnet/efcore#214) are the way to go. You can already add them in 6.0, but there's currently no documentation and you'd need to add an IConventionSetPlugin service implementation.

  6. wdhenrik commented on Apr 20, 2022

    @wdhenrik
    Author
  7. added a commit that references this issue on Nov 12, 2022
    3e16275
  8. added 2 commits that reference this issue on Nov 30, 2022
    a8e088f
    15fb84b
  9. modified the milestones: Backlog, 7.0.0 on Nov 30, 2022
  10. added a commit that references this issue on Feb 6, 2024
    3e14402
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions