public ExecutingAgencyLookupItem CreateNewExecutingAgency(participatingorg org, string userId) { ExecutingAgencyLookupItem returnAgency = null; if (org.ExecutingAgencyTypeId == (int)ExecutingAgencyType.DP) { //http://iatistandard.org/202/codelists/OrganisationType/ var fundsourceCategory = GetOtherFundSourceCategory(userId); var cur = dbContext.tblCurrencies.FirstOrDefault(f => f.IATICode == "USD"); var ent = dbContext.tblFundSources.FirstOrDefault(f => f.FundSourceName == org.Name || f.IATICode == org.@ref); if (ent == null) { ent = new tblFundSource { FundSourceCategoryId = fundsourceCategory.Id, CurrencyId = cur.Id, FundSourceName = org.Name, IATICode = org.@ref, IDate = DateTime.Now, IUser = userId, }; dbContext.tblFundSources.Add(ent); dbContext.SaveChanges(); } returnAgency = new ExecutingAgencyLookupItem { ExecutingAgencyTypeId = (int)ExecutingAgencyType.DP, ExecutingAgencyOrganizationTypeId = ent.FundSourceCategoryId, ExecutingAgencyOrganizationId = ent.Id, Name = ent.FundSourceName, }; } else if (org.ExecutingAgencyTypeId == (int)ExecutingAgencyType.Government) { tblMinistry ministry = GetNAMinistry(userId); var ent = dbContext.tblMinistryAgencies.FirstOrDefault(f => f.AgencyName == org.Name); if (ent == null) { ent = new tblMinistryAgency { MinistryId = ministry.Id, AgencyName = org.Name, IDate = DateTime.Now, IUser = userId, }; dbContext.tblMinistryAgencies.Add(ent); dbContext.SaveChanges(); } returnAgency = new ExecutingAgencyLookupItem { ExecutingAgencyTypeId = (int)ExecutingAgencyType.Government, ExecutingAgencyOrganizationTypeId = ent.MinistryId, ExecutingAgencyOrganizationId = ent.Id, Name = ent.AgencyName, }; } else if (org.ExecutingAgencyTypeId == (int)ExecutingAgencyType.NGO) { var ent = dbContext.tblNGOCSOes.FirstOrDefault(f => f.NGOOrganizationName == org.Name); if (ent == null) { ent = new tblNGOCSO { NGOOrganizationName = org.Name, NGOOrganizationTypeId = dbContext.tblNGOOrganizationTypes.FirstOrDefault().Id, IUser = userId, IDate = DateTime.Now }; dbContext.tblNGOCSOes.Add(ent); dbContext.SaveChanges(); } returnAgency = new ExecutingAgencyLookupItem { ExecutingAgencyTypeId = (int)ExecutingAgencyType.NGO, ExecutingAgencyOrganizationTypeId = ent.NGOOrganizationTypeId, ExecutingAgencyOrganizationId = ent.Id, Name = ent.NGOOrganizationName, }; } return returnAgency; }