c# - ASP.NET MVC 4, EF5, Unique property in model - best practice? -


asp.net mvc 4, ef5, code first, sql server 2012 express

what best practice enforce unique value in model? have places class has 'url' property should unique every place.

public class place {       [scaffoldcolumn(false)]       public virtual int placeid { get; set; }        [displayname("date added")]       public virtual datetime dateadded { get; set; }        [required(errormessage = "place name required")]       [stringlength(100)]       public virtual string name { get; set; }        public virtual string url { get; set; } }; 

why isn't there [unique] data annotation can place on it?

i have seen 1 or 2 discussions on this, no talk of best practice. using code first can somehow tell database set unique constraint on field in database?

what easiest way - , best practice?

as crazy might sound best practice nowadays not use built-in validation , instead use fluentvalidation. code easy read , super-maintainable since validation managed on separate class meaning less spaghetti code.

pseudo-example of trying achieve.

[validator(typeof(placevalidator))] class place {     public int id { get; set; }     public datetime dateadded { get; set; }     public string name { get; set; }     public string url { get; set; } }  public class placevalidator : abstractvalidator<place> {     public placevalidator()     {         rulefor(x => x.name).notempty().withmessage("place name required").length(0, 100);         rulefor(x => x.url).must(beuniqueurl).withmessage("url exists");     }      private bool beuniqueurl(string url)     {         return new datacontext().places.firstordefault(x => x.url == url) == null     } } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -