public static void UpdateSeedDistrictInfo(this DbAppContext context, District districtInfo) { // Adjust the region. int ministry_region_id = districtInfo.Region.MinistryRegionID; var exists = context.Regions.Any(a => a.MinistryRegionID == ministry_region_id); if (exists) { Region region = context.Regions.First(a => a.MinistryRegionID == ministry_region_id); districtInfo.Region = region; } else { districtInfo.Region = null; } District district = context.GetDistrictByMinistryDistrictId(districtInfo.MinistryDistrictID); if (district == null) { context.Districts.Add(districtInfo); } else { district.Name = districtInfo.Name; district.Region = districtInfo.Region; district.StartDate = districtInfo.StartDate; } }