Hey guys. I have a quick question to understand what this means for my project. I am supposed to have my usernames be unique (and enforced at a database level). What does this mean when referring to database level? I am using ASP.net MVC5 with SQL Express as my database.
Asked
Active
Viewed 605 times
1 Answers
2
You are supposed to make certain only unique names are in the database, so you can look at this for more
Basically do
Alter table users add constraint uniq_user unique(username)

James Black
- 41,583
- 10
- 86
- 166
-
Awesome thank you for ansewer, just added it to my t-sql view. It popped up under my keys saying UNQ_UserName (UserName), but it doesnt say unique key around it. Is that ok? – Shawn Feb 13 '14 at 00:51
-
We are using codefirst methods that generate the database, is the any way I can add that constraint to a model? – Shawn Feb 13 '14 at 01:35
-
I expect it won't work, but test and see if you get an error when inserting a duplicate username – James Black Feb 13 '14 at 02:07
-
You may want to see if this helps http://stackoverflow.com/questions/4413084/unique-constraint-in-entity-framework-code-first – James Black Feb 13 '14 at 02:09