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,
27 comments
-
Anonymous
commented
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 Hanano
commented
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 Bradea
commented
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.
-
Bernie
commented
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 bellis
commented
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.
-
Jorge
commented
Please!
-
Per Clausen commented
Simply a must have!
-
Anonymous
commented
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.
-
Oz
commented
please add it
Ican't believe EF still doesnt have it -
Anonymous
commented
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 Carr
commented
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.
-
Dave
commented
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
Please address this issue.
-
Anonymous
commented
Why is this still not implemented... and even not considered for EF 6 !? :(
-
Emil
commented
That's a basic functionality which should be implemented!
-
Todd Morrison
commented
This is hilarious. EF Code-First is behind on a fundamental feature again.
Back to Hibernate.. lolz -
Anonymous
commented
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.
-
AdminDiego Vega
(Admin, DataFx)
commented
@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.
-
Lee
commented
You should really update the blog post from over a year ago.
It's wasting time for people to read through all that only to find out it's not part of EF5. Looks like the feature was postponed?
http://blogs.msdn.com/b/efdesign/archive/2011/03/09/unique-constraints-in-the-entity-framework.aspx.
-
Murad Muradov
commented
It would be helpful, I think.