public static void ValidateApplicationUser( this UrfIdentityDataContext dbContext, DbEntityValidationResult result) { var entity = result.Entry.Entity as ApplicationUser; if (entity == null) { return; } ApplicationUser temp = dbContext .ApplicationUsers.FirstOrDefault(x => x.UserName == entity.UserName); if ((temp != null) && (temp.Id != entity.Id)) { result.ValidationErrors.Add( new DbValidationError( // A {0} with the {1} of '{2}' is already registered ({3}) "UserName", String.Format( ModelValidationResources.NonUniqueField_NoReference, AccountResources.User_Account, AccountResources.UserName, entity.UserName))); } }
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) { if (entityEntry.State != EntityState.Added) { return base.ValidateEntity(entityEntry, items); } var user = entityEntry.Entity as User; // Check for uniqueness of user name if (user == null || !Users.Any(u => String.Equals(u.UserName, user.UserName, StringComparison.CurrentCultureIgnoreCase))) { return base.ValidateEntity(entityEntry, items); } var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); result.ValidationErrors.Add(new DbValidationError("User", "User name must be unique.")); return result; }
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); if (entityEntry.Entity is Application && entityEntry.State == EntityState.Added) { Application app = entityEntry.Entity as Application; if (!app.IsValid) { foreach (ValidationError ve in app.GetValidationErrors()) { result.ValidationErrors.Add(new DbValidationError(ve.PropertyName, ve.ErrorMessage)); } } } if (result.ValidationErrors.Count > 0) { return result; } else { return base.ValidateEntity(entityEntry, items); } }
protected override DbEntityValidationResult ValidateEntity(System.Data.Entity.Infrastructure.DbEntityEntry entityEntry, IDictionary<object, object> items) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); if (entityEntry.Entity is Job && entityEntry.State == System.Data.EntityState.Added) { Job job = entityEntry.Entity as Job; if (Jobs.Where(x => x.name == job.name && x.curr_Revision==job.curr_Revision).Count() > 0) { result.ValidationErrors.Add(new DbValidationError("name", "Job number and revision must be unique.")); Debug.WriteLine("Error thrown: Jobs need to be unique"); } } if (result.ValidationErrors.Count > 0) { return result; } else { Debug.WriteLine("No errors thrown here"); return base.ValidateEntity(entityEntry, items); } }
public static void WriteValiationResults(DbEntityValidationResult result) { Console.WriteLine("Type: {0}", result.Entry.Entity.GetType().Name); Console.WriteLine("Passed Validation: {0}",result.IsValid); foreach (DbValidationError dbValidationError in result.ValidationErrors) { Console.WriteLine("{0}: {1}", dbValidationError.PropertyName, dbValidationError.ErrorMessage); } }
internal void AddErrorsToModelState(DbEntityValidationResult validation, ModelStateDictionary state) { if (validation == null || state == null || validation.IsValid) { return; } var errors = validation.ValidationErrors.Select(r => new { Property = r.PropertyName, ErrorMessage = r.ErrorMessage }); foreach (var error in errors) { ModelState.AddModelError(error.Property, error.ErrorMessage); } }
private static void AppendErrorMessages(DbEntityValidationResult dbEntityValidationResult, StringBuilder stringBuilder) { var entityName = dbEntityValidationResult.Entry.Entity.GetType().Name; foreach (var dbValidationError in dbEntityValidationResult.ValidationErrors) { var propertyName = dbValidationError.PropertyName; var validationError = dbValidationError.ErrorMessage; string error = String.Format(" Property {0} on entity {1} has validtion error : {2}", propertyName, entityName, validationError); stringBuilder.AppendLine(error); } }
// This method ensures that user names are always unique protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) { if (entityEntry.State == EntityState.Added) { User user = entityEntry.Entity as User; // Check for uniqueness of user name if (user != null && Users.Where(u => u.UserName.ToUpper() == user.UserName.ToUpper()).Count() > 0) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); result.ValidationErrors.Add(new DbValidationError("User", "User name must be unique.")); return result; } } return base.ValidateEntity(entityEntry, items); }
protected override DbEntityValidationResult ValidateEntity( System.Data.Entity.Infrastructure.DbEntityEntry entityEntry, IDictionary<object, object> items) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); if (entityEntry.Entity is Employee && entityEntry.State == EntityState.Added) { Employee emp = entityEntry.Entity as Employee; //check for uniqueness of post title if (Employees.Where(e => e.Username == emp.Username).Count() > 0) result.ValidationErrors.Add( new System.Data.Entity.Validation.DbValidationError("Username", "this username is not available") ); } if (result.ValidationErrors.Count() > 0) { return result; } else { return base.ValidateEntity(entityEntry, items); } }
protected override DbEntityValidationResult ValidateEntity(System.Data.Entity.Infrastructure.DbEntityEntry entityEntry, IDictionary<object, object> items) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); if (entityEntry.Entity is MachineInfo && entityEntry.State == EntityState.Modified) { MachineInfo machineInfo = (MachineInfo)entityEntry.Entity; //check for uniqueness of post title if (MachineInfos.Where(m => m.Id == machineInfo.Id).Count() > 0) { result.ValidationErrors.Add( new System.Data.Entity.Validation.DbValidationError("Title", "Appapp id måste vara unikt.")); } } if (entityEntry.Entity is Resident && entityEntry.State == EntityState.Modified) { var resident = (Resident)entityEntry.Entity; //check for uniqueness of post title if (Residents.Any(m => m.Email == resident.Email)) { result.ValidationErrors.Add( new System.Data.Entity.Validation.DbValidationError("Fel", "Appapp mailadressen måste vara unikt.")); } } if (result.ValidationErrors.Count > 0) { return result; } else { return base.ValidateEntity(entityEntry, items); } }
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, System.Collections.Generic.IDictionary<object, object> items) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); //Sender if (entityEntry.Entity is Application) { Application current = entityEntry.Entity as Application; if (current.Sender == "") { result.ValidationErrors.Add(new DbValidationError("Sender", "Application missing Sender!!!")); } } if (entityEntry.Entity is Application) { Application current = entityEntry.Entity as Application; if (current.Sender.Length > 30) { result.ValidationErrors.Add(new DbValidationError("Subject", "Sender cannot be longer than 30 characters!!!")); } } //subject if (entityEntry.Entity is Application) { Application current = entityEntry.Entity as Application; if (current.Subject == "") { result.ValidationErrors.Add(new DbValidationError("Subject", "Application missing subject!!!")); } } if (entityEntry.Entity is Application) { Application current = entityEntry.Entity as Application; if (current.Subject.Length > 30) { result.ValidationErrors.Add(new DbValidationError("Subject", "Subject cannot be longer than 50 characters!!!")); } } //RegNum if (entityEntry.Entity is Application) { Application current = entityEntry.Entity as Application; if (current.Subject.Length > 20) { result.ValidationErrors.Add(new DbValidationError("ReigistrationNumber", "RegistrationNumber cannot be longer than 20 characters!!!")); } } if (entityEntry.Entity is Application) { Application current = entityEntry.Entity as Application; if (current.Sender == "") { result.ValidationErrors.Add(new DbValidationError("Sender", "Application missing registration number!!!")); } } if (result.ValidationErrors.Count > 0) { return result; } else { return base.ValidateEntity(entityEntry, items); } }
private void ValidateInventoryItem(DbEntityValidationResult result) { var inventoryItem = result.Entry.Entity as InventoryItem; if (!Inventories.Any(i => i.InventoryId == inventoryItem.InventoryId)) { result.ValidationErrors.Add(new DbValidationError("InventoryId", "Inventory does not exist in the system")); } if (!Products.Any(p => p.ProductId == inventoryItem.ProductId)) { result.ValidationErrors.Add(new DbValidationError("ProductId", "Product does not exist in the system")); } }
private void ValidateInstitutionBase(DbEntityValidationResult result) { var entity = result.Entry.Entity as InstitutionBase; if ((String.IsNullOrWhiteSpace(entity.DeaLicense) && (result.Entry.Entity.GetType().BaseType == typeof(Manufacturer) || result.Entry.Entity.GetType().BaseType == typeof(Wholesaler))) || (result.Entry.Entity.GetType().BaseType == typeof(Client) && entity.EntityId == Domain.Constants.MasterPharmacyId) || (result.Entry.Entity.GetType().BaseType == typeof(Manufacturer) && entity.EntityId == Domain.Constants.CatchAllManufacturerId)) { return; } if (Clients.Any(c => c.DeaLicense.Trim().Equals(entity.DeaLicense.Trim(), StringComparison.OrdinalIgnoreCase) && c.ClientId != entity.EntityId)) { result.ValidationErrors.Add(new DbValidationError("DeaLicense", "DEA license already exist in the system (client)")); } if (Manufacturers.Any(m => m.DeaLicense.Trim().Equals(entity.DeaLicense.Trim(), StringComparison.OrdinalIgnoreCase) && m.ManufacturerId != entity.EntityId)) { result.ValidationErrors.Add(new DbValidationError("DeaLicense", "DEA license already exist in the system (manufacturer)")); } if (Wholesalers.Any(w => w.DeaLicense.Trim().Equals(entity.DeaLicense.Trim(), StringComparison.OrdinalIgnoreCase) && w.WholesalerId != entity.EntityId)) { result.ValidationErrors.Add(new DbValidationError("DeaLicense", "DEA license already exist in the system (wholesaler)")); } }
public ValidationResult(DbEntityValidationResult result) { _result = result; }
private void ValidateProductForm(DbEntityValidationResult result) { var productForm = result.Entry.Entity as ProductForm; if (ProductForms.Any(p => p.Name.Trim().Equals(productForm.Name.Trim(), StringComparison.OrdinalIgnoreCase) && p.ProductFormId != productForm.ProductFormId)) { result.ValidationErrors.Add(new DbValidationError("Name", "Name already exist in the system")); } }
/// <summary> /// 自定义验证:每一个目的地类Destination下不能有同名的住宿类Lodging /// </summary> /// <param name="result"></param> private void ValidateLodging(DbEntityValidationResult result) { var lodging = result.Entry.Entity as DbContexts.Model.Lodging; if (lodging != null && lodging.DestinationId != 0) { if (Lodgings.Any(l => l.Name == lodging.Name && l.DestinationId == lodging.DestinationId)) { result.ValidationErrors.Add(new DbValidationError("Lodging", "There is already a lodging named " + lodging.Name + " at this destination.")); } } }
private void Validate(DbEntityValidationResult result) { ValidatePerson(result); }
public DbEntityValidationResultBase(System.Data.Entity.Validation.DbEntityValidationResult dbEntityValidationResult) { _dbEntityValidationResult = dbEntityValidationResult; }
private void ValidateClient(DbEntityValidationResult result) { var client = result.Entry.Entity as Client; if (Clients.Any(c => c.Name.Trim().Equals(client.Name.Trim(), StringComparison.OrdinalIgnoreCase) && c.ClientId != client.ClientId)) { result.ValidationErrors.Add(new DbValidationError("Name", "Client name already exist in the system")); } if (Clients.Any(c => c.DeaLicense.Trim().Equals(client.DeaLicense.Trim(), StringComparison.OrdinalIgnoreCase) && c.ClientId != client.ClientId)) { result.ValidationErrors.Add(new DbValidationError("DeaLicense", "DEA license already exist in the system")); } if (Clients.Any(c => c.StateLicense.Trim().Equals(client.StateLicense.Trim(), StringComparison.OrdinalIgnoreCase) && c.ClientId != client.ClientId)) { result.ValidationErrors.Add(new DbValidationError("StateLicense", "State license already exist in the system")); } if (client.WholesalerAccountId.HasValue && !Wholesalers.Any(w => w.WholesalerId == client.WholesalerAccountId)) { result.ValidationErrors.Add(new DbValidationError("WholesalerAccountId", "Wholesaler does not exist in the system")); } }
private void ValidateUserAccount(DbEntityValidationResult result) { var userAccount = result.Entry.Entity as UserAccount; //if (!Enum.IsDefined(typeof(AccessLevel), userAccount.AccessLevel)) //{ // result.ValidationErrors.Add(new DbValidationError("AccessLevel", "Invalid access level")); //} if (!Clients.Any(c => c.ClientId == userAccount.ClientId)) { result.ValidationErrors.Add(new DbValidationError("ClientId", "Invalid associated client")); } if (UserAccounts.Any(ua => ua.EmailAddress.Trim().Equals(userAccount.EmailAddress.Trim(), StringComparison.OrdinalIgnoreCase) && ua.UserAccountId != userAccount.UserAccountId)) { result.ValidationErrors.Add(new DbValidationError("EmailAddress", "Email address already exist in the system")); } if (UserAccounts.Any(ua => ua.UserName.Trim().Equals(userAccount.UserName.Trim(), StringComparison.OrdinalIgnoreCase) && ua.UserAccountId != userAccount.UserAccountId)) { result.ValidationErrors.Add(new DbValidationError("UserName", "Username already exist in the system")); } }
internal static string GetErrorMessage(DbEntityValidationResult validationResult) { return GetErrorMessage(validationResult.ValidationErrors); }
private void ValidateManufacturer(DbEntityValidationResult result) { var manufacturer = result.Entry.Entity as Manufacturer; if (Manufacturers.Any(m => m.Name.Trim().Equals(manufacturer.Name.Trim(), StringComparison.OrdinalIgnoreCase) && m.ManufacturerId != manufacturer.ManufacturerId)) { result.ValidationErrors.Add(new DbValidationError("Name", "Manufacturer name already exist in the system")); } if (!String.IsNullOrWhiteSpace(manufacturer.DeaLicense) && Manufacturers.Any(m => m.DeaLicense.Trim().Equals(manufacturer.DeaLicense.Trim(), StringComparison.OrdinalIgnoreCase) && m.ManufacturerId != manufacturer.ManufacturerId)) { result.ValidationErrors.Add(new DbValidationError("DeaLicense", "DEA license already exist in the system")); } if (Manufacturers.Any(m => m.NdcPrefix.Trim().Equals(manufacturer.NdcPrefix.Trim(), StringComparison.OrdinalIgnoreCase) && m.ManufacturerId != manufacturer.ManufacturerId)) { result.ValidationErrors.Add(new DbValidationError("NdcPrefix", "NDC prefix already exist in the system")); } if (manufacturer.ReturnToManufacturerId.HasValue && !Manufacturers.Any(m => m.ManufacturerId == manufacturer.ReturnToManufacturerId)) { result.ValidationErrors.Add(new DbValidationError("ReturnToManufacturerId", "Return to manufacturer does not exist in the system")); } }
private void ValidateManufacturerPolicy(DbEntityValidationResult result) { var manufacturerPolicy = result.Entry.Entity as ManufacturerPolicy; if (!Manufacturers.Any(m => m.ManufacturerId == manufacturerPolicy.ManufacturerId)) { result.ValidationErrors.Add(new DbValidationError("ManufacturerId", "Manufacturer does not exist in the system")); } }
public void OnValidateEntity(DbEntityValidationResult result, DbEntityEntry entityEntry, IDictionary<object, object> items) { Entries.Add(entityEntry); }
private void ValidateWholesaler(DbEntityValidationResult result) { var wholesaler = result.Entry.Entity as Wholesaler; if (Wholesalers.Any(w => w.Name.Trim().Equals(wholesaler.Name.Trim(), StringComparison.OrdinalIgnoreCase) && w.WholesalerId != wholesaler.WholesalerId)) { result.ValidationErrors.Add(new DbValidationError("Name", "Wholesaler name already exist in the system")); } if (!String.IsNullOrWhiteSpace(wholesaler.DeaLicense) && Wholesalers.Any(w => w.DeaLicense.Trim().Equals(wholesaler.DeaLicense.Trim(), StringComparison.OrdinalIgnoreCase) && w.WholesalerId != wholesaler.WholesalerId)) { result.ValidationErrors.Add(new DbValidationError("DeaLicense", "DEA license already exist in the system")); } }
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) { var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>()); Validate(result); if (!result.IsValid) { return result; } return base.ValidateEntity(entityEntry, items); }
private void ValidateInventory(DbEntityValidationResult result) { var inventory = result.Entry.Entity as Inventory; if (inventory.Status != InventoryStatus.Inventory && String.IsNullOrWhiteSpace(inventory.DebitNumber)) { result.ValidationErrors.Add(new DbValidationError("DebitNumber", "Return must have debit number")); } if (!String.IsNullOrWhiteSpace(inventory.DebitNumber) && Inventories.Any(i => i.DebitNumber.Trim() == inventory.DebitNumber.Trim() && i.InventoryId != inventory.InventoryId)) { result.ValidationErrors.Add(new DbValidationError("DebitNumber", "Debit number already exist in the system")); } }
private void ValidatePerson(DbEntityValidationResult result) { Person po = null; po = result.Entry.Entity as Person; if (po != null) { if (result.Entry.State == EntityState.Added && po.Age < 18) { result.ValidationErrors.Add(new DbValidationError("Person", "La edad no puede ser menor a 18")); } } }
/// <summary> /// 多个验证规则 /// </summary> private void ValidateReservation(DbEntityValidationResult result) { var reservation = result.Entry.Entity as DbContexts.Model.Reservation; if (reservation != null) { if (result.Entry.State == EntityState.Added && reservation.Payments.Count == 0) { result.ValidationErrors.Add(new DbValidationError("Reservation", "New reservation must have a payment.")); } } }
static string DetermineKey( DbContext context, DbEntityValidationResult validationResult ) { var key = context.AsTo<IObjectContextAdapter, ObjectContext>( y => y.ObjectContext ).ExtractKey( validationResult.Entry.Entity ); var result = string.Join( ", ", key.EntityKeyValues.Select( x => x.ToString() ) ); return result; }
private void ValidateProduct(DbEntityValidationResult result) { var product = result.Entry.Entity as Product; if (!product.IsProductIdValid()) { result.ValidationErrors.Add(new DbValidationError("ProductId", "ProductId does not match FullProductId")); } // should we use result.Entry.State == EntityState.Added ? if (Products.Count(p => p.ProductId == product.ProductId) > 1) { result.ValidationErrors.Add(new DbValidationError("ProductId", "Product already exist in the system")); } }