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,
Add Unique Constraint feature
Matt Artz commented
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 Laughlin commented
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 Bullock commented
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?
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
Stijn Herreman commented
Can we get a technical explanation as to why this is so hard to implement?
Martin Costello commented
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).
Please Support is!
Roman Ganz commented
Please add it
Neville Perkins commented
Desperately need this please!
Mohammad Nasir Uddin commented
Please add this feature to the up coming EF version.
Add it please
Patrício dos Santos commented
Add it please...
Mickey Perlstein commented
Unique isn't so complicated, I used Attrbute [Unique()] which works with my tt
Unique constraints in EF is crucial to my app.
I hope you could support Unique Constraints, many people out there would really apreciate that. Cheers and keep up the great work!
Please support Unique Constraint. We really need this feature. Thanks and more power!!
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.