You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by Vasim Hanna-Salem, bugproof, Varorbc, john, stefanolson, kmolerov, Ian Kemp, Roger Hendriks and Daniel Dickerson
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.
Activity
elnoor commentedon Mar 31, 2018
Hi guys, that would be awesome if there was a data annotation for unique constraint.
https://stackoverflow.com/questions/49526370/is-there-a-data-annotation-for-unique-constraint-in-ef-core-code-first
ajcvickers commentedon Oct 15, 2018
See also suggestion in #13627
bricelam commentedon Jan 29, 2019
Related issues: #11003, #10111, #10059 & #4050
ajcvickers commentedon Jan 30, 2019
@divega to consider as a duplicate.
smitpatel commentedon Feb 20, 2019
Duplicate of #10111
ajcvickers commentedon Sep 4, 2019
See also scenarios in #10111
rogerhendriks commentedon Feb 15, 2020
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.
roji commentedon Jun 18, 2020
Removing from backlog to consider closing - we're tracking specific needs in specific issues.
ajcvickers commentedon Jun 22, 2020
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.