Database Reference
In-Depth Information
public partial class EFRecipesEntities
{
partial void OnContextCreated()
{
this.SavingChanges +=new EventHandler(EFRecipesEntities_SavingChanges);
}
private void EFRecipesEntities_SavingChanges(object sender, EventArgs e)
{
var entries = this.ObjectStateManager
.GetObjectStateEntries(EntityState.Added |
EntityState.Modified |
EntityState.Deleted)
.Where(entry => entry.Entity is IValidator)
.Select(entry => entry).ToList();
foreach (var entry in entries)
{
var entity = entry.Entity as IValidator;
entity.Validate(entry);
}
}
}
public interface IValidator
{
void Validate(ObjectStateEntry entry);
}
public partial class SalesOrder : IValidator
{
public void Validate(ObjectStateEntry entry)
{
if (entry.State == EntityState.Added)
{
if (this.OrderDate > DateTime.Now)
throw new ApplicationException(
"OrderDate cannot be after the current date");
}
else if (entry.State == EntityState.Modified)
{
if (this.ShippedDate < this.OrderDate)
{
throw new ApplicationException(
"ShippedDate cannot be before OrderDate");
}
if (this.Shipped.Value && this.Status != "Approved")
{
throw new ApplicationException(
"Order cannot be shipped unless it is Approved");
}
 
Search WWH ::




Custom Search