Question

If I have a dbml file that contains say a Customer class with say a single property of CompanyName;

public partial class Customer : INotifyPropertyChanging, INotifyPropertyChanged
private string _CompanyName;
public string CompanyName
{
  get

Now, given that the above is in a dbml and thus generated I obviously should avoid editing it like the plague.

So I have created another class like so;

public partial class Customer
{
    [Required]
    public string CompanyName{get;set;}
}

The reason for this is because I want to decorate my field as being required.

However this doesn't work as I get the compile error "...already contains a definition for 'CustomerID'".

Does anyone know a way around this or is there a better way to mark fields as required or of validating a model?

Was it helpful?

Solution

The solution is to use a MetadataType attribute with an additional class that contains the data validation annotations.

See: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

OTHER TIPS

Or you could use ViewModel pattern. This also gives you flexibility is binding your model to the view when you want to use things like dropdowns. We use it with AutoMapper.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top