How can we improve Entity Framework?

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,

3,600 votes
Sign in
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 →
    Marco MonducciMarco Monducci shared a merged idea: Columns' uniqueness awareness. (Unique Indexes)  ·   · 


    Sign in
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      • Luciano Evaristo GuercheLuciano Evaristo Guerche commented  ·   ·  Flag as inappropriate

        I am using EF 6.1 and I am sad this feature will be supported only on EF7. I am working with repository advocates who are advert to EF and have no way to convince them with such lack of a feature.

      • shimmyshimmy commented  ·   ·  Flag as inappropriate

        Please also add support for nullable unique key constraints.
        Currently, when attempting to add a second row with a null field, the following error is raised:
        Cannot insert duplicate key row in object 'dbo.People' with unique index 'IX_Passport'. The duplicate key value is (<NULL>).
        The statement has been terminated.

      • Roger HendriksRoger Hendriks commented  ·   ·  Flag as inappropriate

        Mmmm these additions are Migrations only, how about generic error handling when violating a UC? Is that in too?

      • Mike BanteguiMike Bantegui commented  ·   ·  Flag as inappropriate

        If you add this feature, can you also allow Entity Framework to be aware of and create unique constraints when you programmatically create a database instance via Database.Create?

        We have a use case where we have our database versioned and deployed through a secondary mechanism (DbUp) and create the entity model from the deployed database.

        However, some of our projects have functional tests that need to exercise our data access tier. In one specific case, a test written verifying that a particular method correctly handled unique data was erroneously passing because the database created via Database.Create() does not contain any of the unique constraints present in the original DB model.

      • Steve BerwickSteve Berwick commented  ·   ·  Flag as inappropriate

        can't even imagine the nightmare this would be to implement. but thanks for looking into it Diego.

      • Neville PerkinsNeville Perkins commented  ·   ·  Flag as inappropriate

        Yes please!!! We all dream of a world where all Microsoft technologies actually support the same features across the platform. Really bad that SQL has had this for years yet EF which is punted as the main db connection path for app development, does not.

      • LucaLuca commented  ·   ·  Flag as inappropriate

        It was about time!

        Now please add support for unique indexes definition in .edmx Model First..?

      • 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).

      ← Previous 1 3

      Feedback and Knowledge Base