0

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.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Shawn
  • 2,355
  • 14
  • 48
  • 98

1 Answers1

2

You are supposed to make certain only unique names are in the database, so you can look at this for more

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/f7ded633-5d66-4cda-a525-96ab97cee868/sqlexpress-create-unique-column

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