private static void SeedMedicalTransactions(LivestockContext context) { if (context.MedicalTransactions == null || context.MedicalTransactions.Any()) { return; } var animal = context.Animals.OrderBy(a => a.Number).First(); if (animal == null) { SeedAnimals(context); animal = context.Animals.OrderBy(a => a.Number).First(); } context.MedicalTransactions.AddRange( new MedicalTransactionModel() { AnimalId = animal.Id, MedicineId = context.MedicineTypes.OrderBy(m => m.Description).First().Id, TransactionDate = DateTimeOffset.Parse("2021-01-13T16:00:00Z"), UnitId = context.Units.OrderBy(u => u.Description).First().Id, Dose = 0.5m }); context.SaveChanges(); }
private static void SeedFeedingTransactions(LivestockContext livestockContext) { if (livestockContext.FeedingTransactions.Any()) { return; } var animal = livestockContext.Animals.OrderBy(a => a.Number).First(); if (animal == null) { SeedAnimals(livestockContext); animal = livestockContext.Animals.OrderBy(a => a.Number).First(); } livestockContext.FeedingTransactions.AddRange( new FeedingTransactionModel() { AnimalId = animal.Id, FeedTypeId = livestockContext.FeedTypes.OrderBy(f => f.Description).First().Id, Quantity = 0.5m, TransactionDate = DateTimeOffset.Parse("2021-01-13T16:00:00Z"), UnitId = livestockContext.Units.OrderBy(u => u.Description).First().Id }, new FeedingTransactionModel() { AnimalId = animal.Id, FeedTypeId = livestockContext.FeedTypes.OrderBy(f => f.Description).First().Id, Quantity = 1, TransactionDate = DateTimeOffset.Parse("2021-01-12T16:00:00Z"), UnitId = livestockContext.Units.OrderBy(u => u.Description).First().Id }); livestockContext.SaveChanges(); }
private static void SeedAnimals(LivestockContext context) { if (context.Animals == null || context.Animals.Any()) { return; } context.Animals.AddRange( new AnimalModel() { Type = AnimalType.Cattle, Subspecies = "Brahman", Number = 1, BatchNumber = 1, BirthDate = new DateTimeOffset(DateTime.UtcNow.AddDays(-15)), PurchaseDate = new DateTimeOffset(DateTime.UtcNow.AddDays(-13)), PurchasePrice = 200m, ArrivalWeight = 120m, Sold = false, SellPrice = null, Deceased = false }); context.SaveChanges(); }
private static void SeedWeightTransactions(LivestockContext livestockContext) { if (livestockContext.WeightTransactions.Any()) { return; } var animal = livestockContext.Animals.OrderBy(a => a.Number).First(); if (animal == null) { SeedAnimals(livestockContext); animal = livestockContext.Animals.OrderBy(a => a.Number).First(); } livestockContext.WeightTransactions.AddRange( new WeightTransactionModel { AnimalId = animal.Id, TransactionDate = DateTimeOffset.Parse("2021-01-13T16:00:00Z"), Weight = 63 }); livestockContext.SaveChanges(); }
private WeightTransactionCrudService SetUpService(LivestockContext context) { context.SeedTestAnimal(TestConstants.AnimalId1); context.SeedTestAnimal(TestConstants.AnimalId2); context.SeedWeightTransaction(TestConstants.AnimalId1); context.SaveChanges(); return(new WeightTransactionCrudService(_logger, context)); }
/// <inheritdoc/> public void ArchiveAnimals(int[] animalIds) { Logger.LogInformation("Archiving animals..."); var animals = LivestockContext.Animals .Where(animal => animalIds.Contains((int)animal.Id) && !animal.Archived) .ToList(); Logger.LogDebug("Archiving {@Count} unarchived animals...", animals.Count); foreach (var animal in animals) { animal.Archived = true; } LivestockContext.SaveChanges(); }
public void Seed(IServiceProvider serviceProvider) { var options = serviceProvider.GetRequiredService <DbContextOptions <LivestockContext> >(); using var context = new LivestockContext(options); if (_env.IsE2E()) { context.Database.EnsureCreated(); } else { context.Database.Migrate(); } SeedFeedTypes(context); SeedUnits(context); SeedMedicine(context); context.SaveChanges(); }