示例#1
0
 public void AddAuthor(Author author)
 {
     using (Context)
     {
         try
         {
             Context.Authors.Add(author);
             Context.SaveChanges();
         }
         catch
         {
             throw;
         }
     }
 }
示例#2
0
        public ActionResult Create(string firstName, string lastName)
        {
            if (String.IsNullOrEmpty(firstName) || String.IsNullOrEmpty(lastName))
            {
                throw new ArgumentOutOfRangeException();
            }

            var authorRepo = new AuthorRepository();
            var authorToAdd = new Author();

            authorToAdd.FirstName = firstName;
            authorToAdd.LastName = lastName;

            authorRepo.AddAuthor(authorToAdd);

            return RedirectToAction("Index");
        }
示例#3
0
        public void UpdateAuthor(Author author)
        {
            using (Context)
            {
                try
                {
                    //var authorToUpdate = Context.Authors.Find(author.Id);
                    var authorToUpdate = (from a in Context.Authors where a.Id == author.Id select a).FirstOrDefault();

                    if (authorToUpdate != null)
                    {
                        authorToUpdate.Books = author.Books;
                        authorToUpdate.FirstName = author.FirstName;
                        authorToUpdate.LastName = author.LastName;
                        Context.SaveChanges();
                    }
                    else
                        throw new ArgumentException("Object Id not found!");
                }
                catch
                {

                    throw;
                }
            }
        }