Entity Framework Feature Suggestions

Unique Constraint (i.e. Candidate Key) Support

SQL Server and other databases support Unique Constraints on tables. Foreign key constraints are generally based on unique constrains on the principal side, with the Primary Key being only a special case of a unique constraint.
The Entity Framework currently only supports basing referential constraints on primary keys and does not have a notion of a unique constraint. The idea is to have:

• Support for specifying a unique constraint on an Entity
• Support for specifying a foreign key associations that on the principal end specify columns(s) that comprise a unique constraint but are not the primary key,

2,785 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Kati IcevaKati Iceva shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    47 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Matt ArtzMatt Artz commented  ·   ·  Flag as inappropriate

        Yeah, I have just come across this issue after the first part of our modernisation project which was to add surrogate keys as primary keys to our tables (so EF could rely on an unchanging primary key) and set what were previously the primary keys as unique keys then re-added foreign keys from other tables to the unique key. This meant no data or (breaking) schema changes and our legacy applications could continue to work and use joins as they always have and not require modification...

        A large percentage of our entities should have navigational properties through these foreign keys on unique keys but of course we can't do that which means manually retrieving and joining entities until this feature is added.

        Please make it soon!

      • John-Edward Mc LaughlinJohn-Edward Mc Laughlin commented  ·   ·  Flag as inappropriate

        If SQL Server supports this then so should EF. I am using a code first approach and thinking of just moving to a SQL Server database project for my model creation. This has been a request for some time now. Please add this.

      • David BullockDavid Bullock commented  ·   ·  Flag as inappropriate

        A key is a key, whether it's a primary key or not. If I sometimes choose to link based on a natural key instead of a surrogate, what's my Object-Relational binding tool got to object to?

      • Anonymous commented  ·   ·  Flag as inappropriate

        From my point of view this is indeed needed. A table may have candidate keys based on a single field that must be unique. While it is possible to define this in code when the DB is created in a Code First approach (or in the database it self once it has been created), it would be nice if EF can define this in a straightforward way

      • Martin CostelloMartin Costello commented  ·   ·  Flag as inappropriate

        I'd like this supported in the EDMX from database first so that the database can be recreated in an SQL LocalDB instance using DbContext.Database.Initialize() with DropAndCreateDatabaseAlways<T> via Database.SetInitializer<T>() without using SQL scripts just from the model metadata (and similarly with default constraints as well).

      • DeveloperFromMarsDeveloperFromMars commented  ·   ·  Flag as inappropriate

        I hope you could support Unique Constraints, many people out there would really apreciate that. Cheers and keep up the great work!

      • rcnetrcnet commented  ·   ·  Flag as inappropriate

        Please support Unique Constraint. We really need this feature. Thanks and more power!!

      ← Previous 1 3

      Feedback and Knowledge Base