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,

1,450 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 →

    27 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...
      • Anonymous commented  ·   ·  Flag as inappropriate

        Disappointed to figure out what I am trying to achieve with EF5 Code first is not supported. I was under impression any software product always support common requirement like this.

        Also can you please suggest/post a most suitable work around for this problem so that developers who figure out that this functionality is inherently not supported can save some time.

      • Ramy HananoRamy Hanano commented  ·   ·  Flag as inappropriate

        I Wonder why it's not there from version 1.0 :( it's not a feature it's a basic function and can't be omitted.

      • Marcel BradeaMarcel Bradea commented  ·   ·  Flag as inappropriate

        Please guys, this feature is a completely rudimentary part of relational modelling. There are all sorts of scenarios where unique key constraints are essential for any serious database application. Any app out there that has a "business ID" (ie: account number or order number) cannot just expose the "row-version ID" which is what a primary key really represents, and needs to ensure uniqueness of various other non-primary-key columns.

        You really need to take this seriously. It kiddies the maturity of EF for anyone that understands the impact of this limitation. I LOVE the work you've been doing on migrations, code-first and async, but this is a core basic building block that cannot be ignored any longer.

      • BernieBernie commented  ·   ·  Flag as inappropriate

        This simply MUST be included in 6. The feature is absolutely critical when you deal in times-series type data, where composite/unique keys are a way of life.

      • scott bellisscott bellis commented  ·   ·  Flag as inappropriate

        EF can't survive with such a fundamental piece of RDBMS functionality missing from it surely. I've added the capability for now using extension methods on the DbContext plumbed into my migrations for now, but this kind of thing is something you expect to be built in.

      • Anonymous commented  ·   ·  Flag as inappropriate

        I was a bit confused when i found that EF5 didn't support UNIQUE constraints. I currently need to use them on a project that I'm workming now and will have to "hack" something to together to support this.

      • OzOz commented  ·   ·  Flag as inappropriate

        please add it
        Ican't believe EF still doesnt have it

      • Anonymous commented  ·   ·  Flag as inappropriate

        I can't stress enough, please add this. EF is a great tool, but it is shameful that even in it's 6th iteration something basic as Unique Constraint is not supported. Please, please, please but that on the Roadmap for EF6, would be so helpful for many developers.

      • Michael CarrMichael Carr commented  ·   ·  Flag as inappropriate

        You've been doing some great work on EF lately, but this issue is really causing me some headaches. The workarounds are few, ugly, and cause unwanted ripples into the rest of my codebase. Please consider a fix for a future version.

      • DaveDave commented  ·   ·  Flag as inappropriate

        Even if not full blown support for relationships etc, it would be nice to at least be able to declare single and multiple unique columns.

      • Anonymous commented  ·   ·  Flag as inappropriate

        Why is this still not implemented... and even not considered for EF 6 !? :(

      • EmilEmil commented  ·   ·  Flag as inappropriate

        That's a basic functionality which should be implemented!

      • Todd MorrisonTodd Morrison commented  ·   ·  Flag as inappropriate

        This is hilarious. EF Code-First is behind on a fundamental feature again.
        Back to Hibernate.. lolz

      • AnonymousAnonymous commented  ·   ·  Flag as inappropriate

        How difficult can it be to implement this??? Just fenerate uk and pick up the error from db. We made a handler to catch all types of errors and convert them to standards. Even with fk table name converted to entity name.

      • Diego VegaAdminDiego Vega (Admin, DataFx) commented  ·   ·  Flag as inappropriate

        @Lee: thanks for your feedback and your patience with this. I have updated the blog post with a comment at the top saying the feature was postponed and won't be included in Entity Framework 5.

      ← Previous 1

      Feedback and Knowledge Base