문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top