static void CopyOrgs() { using (var dgis = new DGisContext("Томск")) { using (var transportCtx = new TransportContext()) { Console.Write("Копирование категорий предприятий... "); var dgisOrgRubs1 = dgis.OrgRubs1.GetAll(); var orgRubs = new List <OrgRub>(); foreach (var orgRub1 in dgisOrgRubs1) { orgRubs.Add(new OrgRub { Name = orgRub1.Name, ParentOrgRub = null }); } var dgisOrgRubs2 = dgis.OrgRubs2.GetAll(); foreach (var orgRub2 in dgisOrgRubs2) { if (orgRubs.Any(rub => rub.Name == orgRub2.Name)) { continue; } var parentRubName = dgis.OrgRubs1.GetById(orgRub2.OrgRub1Id).Name; orgRubs.Add(new OrgRub { Name = orgRub2.Name, ParentOrgRub = orgRubs.FirstOrDefault(rub => rub.Name == parentRubName) }); } var dgisOrgRubs3 = dgis.OrgRubs3.GetAll(); foreach (var orgRub3 in dgisOrgRubs3) { if (orgRubs.Any(rub => rub.Name == orgRub3.Name)) { continue; } var parentRubName = dgis.OrgRubs2.GetById(orgRub3.OrgRub2Id).Name; orgRubs.Add(new OrgRub { Name = orgRub3.Name, ParentOrgRub = orgRubs.FirstOrDefault(rub => rub.Name == parentRubName) }); } transportCtx.OrgRubs.AddRange(orgRubs); transportCtx.SaveChanges(); Console.WriteLine("завершено"); Console.Write("Копирование предприятий... "); var dgisOrgs = dgis.Orgs.GetAll(); foreach (var dgisOrg in dgisOrgs) { var org = new Org { Name = dgisOrg.Name, }; foreach (var orgRubs3Id in dgisOrg.OrgRubs3Ids) { var dgisOrgRub3 = dgis.OrgRubs3.GetById(orgRubs3Id); var orgRub = transportCtx.OrgRubs.FirstOrDefault(rub => rub.Name == dgisOrgRub3.Name); if (orgRub == null) { continue; } org.OrgRubs.Add(orgRub); } foreach (var dgisOrgFil in dgisOrg.OrgFils) { var orgFil = new OrgFil { Org = org }; foreach (var addressesId in dgisOrgFil.AddressesIds) { var dgisAddress = dgis.Addresses.GetById(addressesId); if (dgisAddress.BuildingId == null) { continue; } var address = transportCtx.Addresses.FirstOrDefault(addr => addr.Number == dgisAddress.Number && addr.Street == dgisAddress.Street); if (address == null) { continue; } orgFil.BuildingId = address.BuildingId; break; } if (orgFil.BuildingId == 0) { continue; } org.OrgFils.Add(orgFil); } transportCtx.Orgs.Add(org); } transportCtx.SaveChanges(); Console.WriteLine("завершено"); } } }
protected override Org RowToEntity(IDataRow row) { var org = new Org { OrgId = row.Index, Name = (string)row.Value["name"] }; var filCount = (int)row.Value["fil_count"]; for (var i = 1; i <= filCount; i++) { var filRow = (IDataRow)row.Value["fil_" + i]; if (filRow == null) { continue; } var fil = new OrgFil { OrgFilId = filRow.Index, OrgId = row.Index }; var filRubsCount = (int)filRow.Value["rub_count"]; for (var j = 1; j <= filRubsCount; j++) { var rub = (IDataRow)filRow.Value["rub_" + j]; if (rub == null) { continue; } fil.OrgRubs3Ids.Add(rub.Index); } var filAddrCount = (int)filRow.Value["addr_count"]; for (var j = 1; j <= filAddrCount; j++) { var addr = (IDataRow)filRow.Value["addr_" + j]; if (addr == null) { continue; } fil.AddressesIds.Add(addr.Index); } org.OrgFils.Add(fil); } var rubCount = (int)row.Value["rub_count"]; for (var i = 1; i <= rubCount; i++) { var rub = (IDataRow)row.Value["rub_" + i]; if (rub == null) { continue; } org.OrgRubs3Ids.Add(rub.Index); } return(org); }