Skip to content

Create EF-specific mapping attributes ('data annotations') #10864

Closed as not planned

Description

@ajcvickers

Using C# attributes to drive EF mapping is very popular. Currently, we use data annotations (System.ComponentModel.DataAnnotations) for this, which has a couple of advantages:

  • The attributes are not coupled to the EF assemblies, so referencing EF from the domain model is not required to use them.
  • Multiple parts of an application can read the same annotations and act accordingly--e.g. Required for validation and Required for making the database relationship required.

However, there are also several disadvantages:

  • Sometimes the meaning that EF takes from the attribute is not exactly the same as the meaning other parts of the application take--for example, Required for validation and Required for making the database relationship required. This then requires use of the fluent API to override the annotation meaning.
  • Getting new attributes or changes to existing attributes into .NET Core and having those changes propagate through to the .NET Framework as well can be a slow process, and there is a high compat bar.
  • There are a lot of places whether the annotation semantics are very database-specific. Putting these semantics in general non-database-specific attributes is a bad fit.

Because of this, we have already created a new attributes package: EFCore.Attributes. This contains new attributes like OwnedAttribute. This package will not have any references to the main EF Core assemblies so it can be included in a domain model with minimal impact--to avoid this entirely, use the fluent API.

This issue is about creating new, EF-specific, attributes even for concepts already covered by the data annotations. This would then allow higher fidelity mapping for EF Core using attributes instead of the fluent API, with all attributes in a single location that can be iterated on and released as EF Core evolves.

Note that this does not mean that we will stop honoring data annotations--that will keep working--but there will be another option with more EF-specific attributes that can be used optionally.

Activity

  1. added this to the Backlog milestone on Feb 3, 2018
  2. elnoor commented on Mar 31, 2018

    @elnoor
  3. ajcvickers commented on Oct 15, 2018

    @ajcvickers
    ContributorAuthor

    See also suggestion in #13627

  4. bricelam commented on Jan 29, 2019

    @bricelam
    Contributor

    Related issues: #11003, #10111, #10059 & #4050

  5. ajcvickers commented on Jan 30, 2019

    @ajcvickers
    ContributorAuthor

    @divega to consider as a duplicate.

  6. smitpatel commented on Feb 20, 2019

    @smitpatel
    Contributor

    Duplicate of #10111

  7. added
    good first issue
    on Sep 2, 2019
  8. ajcvickers commented on Sep 4, 2019

    @ajcvickers
    ContributorAuthor

    See also scenarios in #10111

  9. added and removed
    good first issue
    on Sep 18, 2019
  10. self-assigned this
    on Nov 20, 2019
  11. rogerhendriks commented on Feb 15, 2020

    @rogerhendriks

    Sorry to see this isn't included in the v5 planning. Stays a matter of personal preference but i like the clean approch of annotations.

  12. removed this from the Backlog milestone on Jun 18, 2020
  13. roji commented on Jun 18, 2020

    @roji
    Member

    Removing from backlog to consider closing - we're tracking specific needs in specific issues.

  14. ajcvickers commented on Jun 22, 2020

    @ajcvickers
    ContributorAuthor

    During 5.0 we have agreed on the approach decided here and are executing on it. Given that we have decided that this is the way to go we will track attributes individually and no longer need this overall tracking issue.

  15. removed their assignment
    on Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions