public void Seed(ILogger <AppDbContext> logger, string scriptsPath) { if (Companies.Any()) { return; } var scripts = new string [] { Path.Combine(scriptsPath, "countys.sql"), Path.Combine(scriptsPath, "citys.sql"), Path.Combine(scriptsPath, "districts.sql") }; bool allFilesExists = scripts.Select(x => File.Exists(x) ? 0 : 1).Sum() == 0; if (allFilesExists) { using (var transaction = Database.BeginTransaction()) { foreach (var file in scripts) { var fileContent = System.IO.File.ReadAllLines(Path.Combine(scriptsPath, file)); foreach (var statement in fileContent) { this.Database.ExecuteSqlCommand(new RawSqlString(statement)); } } transaction.Commit(); } } else { logger.LogCritical("Scripts (Endereços) do banco de dados não encontrados"); } #region Data Test using (var transaction = Database.BeginTransaction()) { int companyId = 1; int clientId = 1; var unit = new Repository.UnitWork(new Repository.UnitWorkInfo { CompanyId = companyId }, this, null); Companies.Add(new CompanyEty { Name = "Company Test", CpfCnpj = "00011122299", Email = "*****@*****.**", Phone = "33900112233" }); SaveChanges(); Users.Add(new UserEty { Id = unit.GenereteId("Users"), CompanyId = companyId, Name = "User Test", Email = "*****@*****.**", Password = "******" }); SaveChanges(); Clients.Add(new ClientEty { Id = unit.GenereteId("Clients"), CompanyId = companyId, Name = "Client Test", Email = "*****@*****.**", CpfCnpj = "11122233388", Phone = "33900886655", ContactName = "C Test", RgIE = "AA 12120000", Org = "AA", Address = new AddressEty { CountyId = 31, CountyName = "Minas Gerais", CountyInitials = "MG", CityId = 3127701, CityName = "GV", DistrictId = 0, DistrictName = "Centro", Number = "60", Street = "AV Minas Gerais", ZipCode = "35060000", } }); SaveChanges(); Services.Add(new ServiceEty { Id = unit.GenereteId("Services"), CompanyId = companyId, Name = "Service Test", Rate = 10, PlateCard = 5, Honorary = 30, Other = 1 }); SaveChanges(); Vehicles.Add(new VehicleEty { Id = unit.GenereteId("Vehicles"), CompanyId = companyId, ClientId = clientId, Model = "Model test", Plate = "AAA0000", Color = "Red", Chassis = "CHS 000", Renavam = "RNV 000", ModelYear = 2019, YearManufacture = 2019, CountyId = 31, CountyName = "Minas Gerais", CountyInitials = "MG", CityId = 3127701, CityName = "Governador Valadares", Type = VehicleTypeEty.MotorCycle }); SaveChanges(); transaction.Commit(); } #endregion }
// // --------------------------------------------------------------------------- // Insert duplicate validation. // protected override DbEntityValidationResult ValidateEntity( DbEntityEntry entityEntry, IDictionary <object, object> items) { if (entityEntry.Entity is ApplicationUser && entityEntry.State == EntityState.Added) { if (Users.Any(a => a.UserName == ((ApplicationUser)entityEntry.Entity).UserName || a.Email == ((ApplicationUser)entityEntry.Entity).Email || a.FullName == ((ApplicationUser)entityEntry.Entity).FullName )) { // return validation error return(new DbEntityValidationResult(entityEntry, new List <DbValidationError>() { new DbValidationError("User", string.Format("Duplicate User: '******' or '{1}' or '{2}'", ((ApplicationUser)entityEntry.Entity).UserName, ((ApplicationUser)entityEntry.Entity).Email, ((ApplicationUser)entityEntry.Entity).FullName)) })); } } if (entityEntry.Entity is Company && entityEntry.State == EntityState.Added) { if (Companies.Any(a => a.CompanyName == ((Company)entityEntry.Entity).CompanyName)) { // return validation error return(new DbEntityValidationResult(entityEntry, new List <DbValidationError>() { new DbValidationError("Company", string.Format("Duplicate Company: '{0}'", ((Company)entityEntry.Entity).CompanyName)) })); } } // if (entityEntry.Entity is IncidentType && entityEntry.State == EntityState.Added) { if (IncidentTypes.Any(a => a.IncidentTypeShortDesc == ((IncidentType)entityEntry.Entity).IncidentTypeShortDesc || a.IncidentTypeDesc == ((IncidentType)entityEntry.Entity).IncidentTypeDesc )) { // return validation error return(new DbEntityValidationResult(entityEntry, new List <DbValidationError>() { new DbValidationError("IncidentType", string.Format("Duplicate IncidentType: '{0}' or '{1}'", ((IncidentType)entityEntry.Entity).IncidentTypeShortDesc, ((IncidentType)entityEntry.Entity).IncidentTypeShortDesc)) })); } } // if (entityEntry.Entity is NIC && entityEntry.State == EntityState.Added) { if (NICs.Any(a => a.NIC_Id == ((NIC)entityEntry.Entity).NIC_Id || a.NICDescription == ((NIC)entityEntry.Entity).NICDescription )) { // return validation error return(new DbEntityValidationResult(entityEntry, new List <DbValidationError>() { new DbValidationError("NIC", string.Format("Duplicate NIC: '{0}' or '{1}'", ((NIC)entityEntry.Entity).NIC_Id, ((NIC)entityEntry.Entity).NICDescription)) })); } } if (entityEntry.Entity is NoteType && entityEntry.State == EntityState.Added) { if (NoteTypes.Any(a => a.NoteTypeDesc == ((NoteType)entityEntry.Entity).NoteTypeDesc)) { // return validation error return(new DbEntityValidationResult(entityEntry, new List <DbValidationError>() { new DbValidationError("NoteType", "Duplicate NoteType: '" + ((NoteType)entityEntry.Entity).NoteTypeDesc + "'") })); } } // return(base.ValidateEntity(entityEntry, items)); }
public void TabSelectedChanged(int index) { TabIndex = index; Search(); IsEmpty = !(index == 0 ? Contacts != null && Contacts.Any() : Companies != null && Companies.Any()); }