public bool Add(CompanyDto entity) //adding a company { try { var company = new Company { CompanyName = entity.CompanyName, Turnover = entity.Turnover, CEO = entity.CEO, BoardOfDirectors = entity.BoardOfDirectors, Brief = entity.Brief //we are also getting stock exchange ID here }; company.Sector = context.Sectors.Find(entity.SectorId); var sector = context.Sectors.Find(entity.SectorId); context.Companies.Add(company); //ICollection<Company> a = new HashSet<Company>(); //a.Add(company); sector.Companies.Add(company); context.Sectors.Update(sector); //sector.Companies.Add(company); //company.CompanyId get this // do the below via adding object of StockExchangeCompanies // StockExchangeCompanies = context.StockExchangeCompanies.Add(company.CompanyId, company.StockExchangeId) for (int i = 0; i < entity.StockExchangeIds.Count(); i++) { var sid = entity.StockExchangeIds[i]; var sec = new StockExchangeCompany { Company = company, StockExchange = context.StockExchanges.Find(sid) }; context.StockExchangeCompanies.Add(sec); } context.SaveChanges(); return(true); } catch (Exception ex) { throw ex; } }
public bool Add(int id, int id2) { var se = context.StockExchanges.Find(id); //Check if the seany exists or not too. Assuming that it does, the code shall be as follows var entity = context.Companies.Find(id2); //var ipo = context.IPODetails.Single(s => s.StockExchangeCompany.StockExchangeId == id && s.StockExchangeCompany.CompanyId == id2); var sec = new StockExchangeCompany { Company = entity, StockExchange = se }; context.StockExchangeCompanies.Add(sec); var res = context.SaveChanges(); if (res > 0) { return(true); } return(false); }