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 GenericRepository(AuthorsDbContext context) { _context = context; _dbSet = _context.Set <T>(); }
public AuthorsRepository(AuthorsDbContext context) { _context = context; }