public void Delete(T item) { if (_context.Entry(item).State == EntityState.Detached) { _dbSet.Attach(item); } _dbSet.Remove(item); _context.SaveChanges(); }
public ActionResult InsertPeople() { ViewBag.Message = "Insert People"; //string path = "c:/MACA/persons-modified.txt"; string path = ""; // To prevent bad actions! StreamReader srPeople = new StreamReader(path); List <Person> lstPeople = new List <Person>(); while (!srPeople.EndOfStream) { string[] lineArr = srPeople.ReadLine().Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); Person person = new Person(); person.PersonID = Guid.NewGuid(); person.AISID = lineArr[0].Trim(); person.Name = lineArr[1].Trim(); person.Surname = lineArr[2].Trim(); person.FullName = lineArr[3].Trim(); lstPeople.Add(person); } srPeople.Close(); // Insert Persons PersonsDbContext dbPersons = new PersonsDbContext(); AuthorsDbContext dbAuthors = new AuthorsDbContext(); foreach (Person person in lstPeople) { person.DateCreated = DateTime.Now; person.DateModified = DateTime.Now; person.UserCreatedID = User.Identity.GetUserId(); person.UserModifiedID = person.UserCreatedID; dbPersons.Persons.Add(person); dbPersons.SaveChanges(); // Automatically add a person to authors Author author = new Author(); author.AuthorID = Guid.NewGuid(); author.Surname = person.Surname; author.FirstName = person.Name; author.DateCreated = DateTime.Now; author.DateModified = DateTime.Now; author.UserCreatedID = new Guid(User.Identity.GetUserId()); author.UserModifiedID = author.UserCreatedID; dbAuthors.Authors.Add(author); dbAuthors.SaveChanges(); Person personTmp = dbPersons.Persons.Find(person.PersonID); personTmp.AuthorID = author.AuthorID; dbPersons.Entry(personTmp).State = EntityState.Modified; dbPersons.SaveChanges(); } return(RedirectToAction("Administration")); }
public ActionResult AuthorsCreate([Bind(Include = "Surname,FirstName,CREPCCode,ORCID")] Author model) { if (ModelState.IsValid) { model.AuthorID = Guid.NewGuid(); model.DateCreated = DateTime.Now; model.DateModified = model.DateCreated; model.UserCreatedID = Guid.Parse(User.Identity.GetUserId()); model.UserModifiedID = model.UserCreatedID; dbAuthors.Authors.Add(model); dbAuthors.SaveChanges(); return(RedirectToAction("AuthorsIndex")); } return(View(model)); }
public ActionResult Create([Bind(Include = "Surname,Name,FullName,Description,Image")] PersonViewModel personView) { if (ModelState.IsValid) { Guid authorGuid = Guid.NewGuid(); Person person = new Person(); person.PersonID = Guid.NewGuid(); person.Surname = personView.Surname; person.Name = personView.Name; person.FullName = personView.FullName; person.Description = personView.Description; person.DateCreated = DateTime.Now; person.DateModified = DateTime.Now; person.UserCreatedID = User.Identity.GetUserId(); person.UserModifiedID = User.Identity.GetUserId(); // Handle the image if (personView.Image != null && personView.Image.ContentLength > 0) { if (!Auxiliaries.ValidImageTypes.Contains(personView.Image.ContentType)) { ModelState.AddModelError("Image", "Choose an image in one of the following formats: GIF, JPG, or PNG."); } else { using (var reader = new BinaryReader(personView.Image.InputStream)) { person.Image = reader.ReadBytes(personView.Image.ContentLength); } } } dbPersons.Persons.Add(person); dbPersons.SaveChanges(); // Automatically add a person to authors Author author = new Author(); author.AuthorID = authorGuid; author.Surname = person.Surname; author.FirstName = person.Name; author.DateCreated = DateTime.Now; author.DateModified = DateTime.Now; author.UserCreatedID = new Guid(User.Identity.GetUserId()); author.UserModifiedID = author.UserCreatedID; dbAuthors.Authors.Add(author); dbAuthors.SaveChanges(); person = dbPersons.Persons.Find(person.PersonID); person.AuthorID = authorGuid; dbPersons.Entry(person).State = EntityState.Modified; dbPersons.SaveChanges(); return(RedirectToAction("Index")); } return(View(personView)); }