I'm writing a WPF MVVM Light app using Visual Studio 2015. The data has been brought in using Entity Framework 6, using database-first to generate the models. In my MainViewModel.cs file, I'd like to validate the data before doing a SaveChanges()
.
The examples I've seen talk about adding annotations to models (for example, this); however, I'm using auto-generated Entity Framework models. And my ViewModels reference ObservableCollection<Employee>
objects -- nothing references fields directly, so that I can put annotations on them.
Here is the SearchResults
property, which holds the results returned from EF:
private ObservableCollection<Employee> _searchResults;
public ObservableCollection<Employee> SearchResults
{
get { return _searchResults; }
set
{
if (_searchResults == value) return;
_searchResults = value;
RaisePropertyChanged("SearchResults");
}
}
The SearchResults
gets populated after a search and are bound to a DataGrid:
var query = Context.Employees.AsQueryable();
// Build query here...
SearchResults = new ObservableCollection<Employee>(query.ToList());
The user clicks a row on the DataGrid and we show the details for them to update. They can then hit the Save button. But we want to validate the fields in each Employee
before performing Context.SaveChanges()
.
Here's the pertinent area of the partial class Employee
, auto-generated by Entity Framework:
public int employeeID { get; set; }
public int securityID { get; set; }
public string firstName { get; set; }
public string middleName { get; set; }
public string lastName { get; set; }
public string suffix { get; set; }
public string job { get; set; }
public string organizationalUnit { get; set; }
public string costCenter { get; set; }
public string notes { get; set; }
public System.DateTime createdDate { get; set; }
For example, the securityID
must not be blank and it must be an int
, while firstName
and lastName
are required, etc. How do you accomplish this validation and show errors to the user?