Question

In my ASP.NET web site users model, I want both Guid Id's and String UserNames to be unique. I am going to map my model objects to a DB using Entity Framework.

Is there a build-in attribute or some other mechanism that facilitates this common constraint in ASP.NET MVC? Or am I bound to manually checking user name uniqueness at user register form?

Was it helpful?

Solution

Firstly, in my opinion, MVC, or your front end application shouldn't be responsible for doing this.

Unique Id and unique Username are the constraints that should be enforced on the database, otherwise, you are making an assumption that the only way to register users is from your website.

Entity framework has attributes for enforcing unique constraints. More info here: http://blogs.msdn.com/b/efdesign/archive/2011/03/09/unique-constraints-in-the-entity-framework.aspx

Finally, regarding the Ids of your users, I would consider auto generating these within the database, rather then your front end application.

OTHER TIPS

There could be something built-in, but checking for unique values is a line or two since you're using EF.

bool uniqueGuid = Users.Any(u => u.Id == "guid");
bool uniqueUser = Users.Any(u => u.UserName == "username");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top