public bool Save(Company Entity) { _context.Companies.Add(Entity); return true; }
public ActionResult CompanyEntry(HttpPostedFileBase file,CompanyTopic company) { // This needs to save the file, get the path form the topic and save both the topic and the company // Here we save the file as well as we have all the information to process // both topics and company data so all these objects need to be formed and save here if not server side errors // should be thrown... string path=""; int idCompany=0; int flag = 0; //change this function to add new images in to the new table and //related image id into the topics table below ; Guid imageGuid = Guid.NewGuid(); if (file != null && file.ContentLength > 0) try { var comp = new Company { CompanyName = company.CompanyName }; _companyRepo.Save(comp); //save the passed image; //if (!_companyRepo.GetAll().Any()) //{ _iUnitOfWork.Commit(); flag = 1; //} idCompany = _companyRepo.GetAll().OrderByDescending(p => p.Id).FirstOrDefault().Id; //_topicRepo.GetAll().OrderByDescending(p => p.Id).FirstOrDefault().Id; path = flag==1? Path.Combine(Server.MapPath("~/Content/Images"), "Company" + idCompany + Path.GetExtension(file.FileName)) : Path.Combine(Server.MapPath("~/Content/Images"), "Company" + ++idCompany + Path.GetExtension(file.FileName)); file.SaveAs(path); // path to be save to the database; path = "/Content/Images/Company" + idCompany + Path.GetExtension(file.FileName); ViewBag.Message = "File uploaded successfully"; } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message; } else { ViewBag.Message = "You have not specified a file."; } var listing = new Topic { Body = company.Body, CompanyId = idCompany, Created = DateTime.Now, // isVisible = true, //this will need to be retrieved from image table from now on //F ImageId = imageGuid, Title = company.Title, // ProductCategory = "UnCategorized" }; _topicRepo.Save(listing); _iUnitOfWork.Commit(); var image = new EF.Core.Image { ImageId = imageGuid, ImagePath = path, TopicId = _topicRepo.GetTopicByImageId(imageGuid).Id }; var imageRepo = _iUnitOfWork.Images; imageRepo.Save(image); _iUnitOfWork.Commit(); //Now add the path to the //redirect to the created company's page return RedirectToAction("Index", routeValues: new { id = idCompany }); }