private List <Pet> SeedPets() { _logger.LogInformation("Seeding Pets"); var pets = new List <Pet> { new Pet { Uuid = Guid.Parse("f71ba646-bd4f-4220-ad16-c54a54b5a2c4"), Name = "Leo", BirthDate = DateTime.Parse("2015-09-07T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == GeorgeFranklinUuid), Type = PetType.Cat, Visits = new List <Visit> { new Visit { Uuid = Guid.NewGuid(), Date = DateTime.Parse("2015-09-25T13:00:00Z"), AssignedVet = Vets[0], Notes = "Rabies shot" }, new Visit { Uuid = Guid.NewGuid(), Date = DateTime.Parse("2016-09-25T14:30:00Z"), AssignedVet = Vets[0], Notes = "Neuter" } } }, new Pet { Uuid = Guid.Parse("f08bf54f-a429-49e4-8750-78bd00e8bb80"), Name = "Basil", BirthDate = DateTime.Parse("2017-01-22T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == BettyDavisUuid), Type = PetType.Hamster, Visits = new List <Visit> { new Visit { Uuid = Guid.NewGuid(), Date = DateTime.Parse("2017-01-30T20:30:00Z"), AssignedVet = Vets[1], Notes = "Shots" } } }, new Pet { Uuid = Guid.Parse("6c0c6747-9b92-4ddc-ac35-32de088fef47"), Name = "Rosy", BirthDate = DateTime.Parse("2007-04-17T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == EduardoRodriquezUuid), Type = PetType.Dog, Visits = new List <Visit> { new Visit { Uuid = new Guid(), Date = DateTime.Parse("2007-05-05T13:00:00Z"), AssignedVet = Vets[5], Notes = "Shots" } } }, new Pet { Uuid = Guid.Parse("11a22010-a624-43fa-aa0e-79f65b9c0d38"), Name = "Jewel", Type = PetType.Dog, Owner = Owners.Find(o => o.Uuid == EduardoRodriquezUuid), BirthDate = DateTime.Parse("2017-05-25T00:00:00Z"), }, new Pet { Uuid = Guid.Parse("5ab90d5d-fcd8-4090-bc87-eb256fc14eb1"), Name = "Iggy", Type = PetType.Lizard, Owner = Owners.Find(o => o.Uuid == HaroldDavisUuid), BirthDate = DateTime.Parse("2019-01-02T00:00:00Z") }, new Pet { Uuid = Guid.Parse("f8dab28f-1c86-4041-851e-491a0f2ba731"), Name = "George", Type = PetType.Snake, BirthDate = DateTime.Parse("2018-12-14T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == PeterMcTavishUuid), }, new Pet { Uuid = Guid.Parse("bcc3632e-24a2-4e11-a25a-b2fa1acb0ff4"), Name = "Samantha", Type = PetType.Cat, BirthDate = DateTime.Parse("2007-07-10T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == JeanColemanUuid) }, new Pet { Uuid = Guid.Parse("710038dd-9cc0-4a6a-ae5d-a7930db790b4"), Name = "Max", Type = PetType.Dog, BirthDate = DateTime.Parse("2009-02-14T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == JeanColemanUuid), }, new Pet { Uuid = Guid.Parse("66dcf9be-d5f0-4939-9890-1038f96eec26"), Name = "Lucky", Type = PetType.Bird, BirthDate = DateTime.Parse("2016-08-18T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == JeffBlackUuid) }, new Pet { Uuid = Guid.Parse("6feccb06-0b0a-4404-9c15-7459a34e93a3"), Name = "Mulligan", Type = PetType.Dog, BirthDate = DateTime.Parse("2014-05-17T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == MariaEscobitoUuid) }, new Pet { Uuid = Guid.Parse("88905de4-f7f2-4fee-87d6-6340feeaeb35"), Name = "Freddy", Type = PetType.Bird, BirthDate = DateTime.Parse("2016-11-27T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == DavidSchroederUuid) }, new Pet { Uuid = Guid.Parse("a3bfce82-bce2-4b07-9635-4d140e290323"), Name = "Lucky", Type = PetType.Dog, BirthDate = DateTime.Parse("2012-12-12T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == CarlosEstabanUuid) }, new Pet { Uuid = Guid.Parse("e9bf1f05-5faa-4dbc-9c6d-7ec61978c888"), Name = "Sly", Type = PetType.Snake, BirthDate = DateTime.Parse("2011-09-11T00:00:00Z"), Owner = Owners.Find(o => o.Uuid == CarlosEstabanUuid) }, }; var petUuids = pets.Select(p => p.Uuid); var foundPets = DbContext.Pets.Where(p => petUuids.Contains(p.Uuid)).ToImmutableList(); var fetchedPets = pets.Select(pet => { var found = foundPets.FirstOrDefault(p => p.Uuid == pet.Uuid); return(found ?? pet); }).ToList(); var toInsert = fetchedPets.Where(p => p.Id == 0).ToImmutableList(); if (toInsert.Any()) { _logger.LogInformation($"Will insert {toInsert.Count} pets"); DbContext.AddRange(toInsert); } else { _logger.LogInformation("No pets to insert"); } return(fetchedPets); }