Entity Framework code first unique column

In Entity Framework 6.1+ you can use this attribute on your model: [Index(IsUnique=true)] You can find it in this namespace: using System.ComponentModel.DataAnnotations.Schema; If your model field is a string, make sure it is not set to nvarchar(MAX) in SQL Server or you will see this error with Entity Framework Code First: Column ‘x’ in table … Read more

Disable Required validation attribute under certain circumstances

This problem can be easily solved by using view models. View models are classes that are specifically tailored to the needs of a given view. So for example in your case you could have the following view models: public UpdateViewView { [Required] public string Id { get; set; } … some other properties } public … Read more

displayname attribute vs display attribute

DisplayName sets the DisplayName in the model metadata. For example: [DisplayName(“foo”)] public string MyProperty { get; set; } and if you use in your view the following: @Html.LabelFor(x => x.MyProperty) it would generate: <label for=”MyProperty”>foo</label> Display does the same, but also allows you to set other metadata properties such as Name, Description, … Brad Wilson … Read more

Fluent Validation vs. Data Annotations

I prefer Fluent Validation: It gives me far better control of my validation rules Doing conditional validation on different properties is so much easier compared to Data Annotations It separates the validation from my view models Unit testing is far easier compared to Data Annotations It has excellent client side validation support for most standard … Read more