Skip to main content

Posts

Showing posts from April, 2012

Asp.net mvc 3 DataAnnotation using IValidatableObject for multiple properties.

If you have a scenario in which you want to base a validation on multiple business logics on multiple properties then you should consider using IValidatableObject . One of the advantages is that you can use the properties attributes directly which open door to a lot of validation possibilities. It will only be called when there are no individual properties error. It doesn't support clientside validation. Below is the simple code that will give you insight of how this all works. public class User : IValidatableObject { [Key] public string UserId { get; set; } [Required] public string UserName { get; set; } [Required] public string Password { get; set; } [Compare("Password")] public string ConfirmPassword { get; set; } public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { List<ValidationResult> err = new List<ValidationResult&

Asp.net mvc entity framework code first appoach.

Entity Framework comes with three approaches for development. 1) Database First: In this approach you already have your database and the entity framework will generate the model for you. 2) Model First: You don't have the database in this approach and you manually create the domain model using the entity framework designer. After creating the model the designer will generate the ddl statement which will be used to create the database. 3) Code First which I am covering in this post was introduced in EF 4.1. Code first allows you to create the domain model in the code without using the designer. After that EF will be generating the database for you. This approach is used only if you don't have the database the first time. If the database exists use the first approach. So here how the things goes. public class User { [Key] public int UserID { get; set; } [Required] public string UserName { get; set; } [Required] public string Password { get; set;