Exemplo n.º 1
0
 public static void SaveAuthor(AuthorEntity detail)
 {
     using (LMSEntities dbe = new DAL.LMSEntities())
     {
         Author author = new DAL.Author();
         author.Id            = detail.Id;
         author.FirstName     = detail.FirstName;
         author.LastName      = detail.LastName;
         author.Phone         = detail.Phone;
         author.Email         = detail.Email;
         author.Qualification = detail.Qualification;
         dbe.Authors.Add(author);
         dbe.SaveChanges();
     }
 }
        public string AddOrUpdateAuthor(Author author)
        {
            Table <DAL.Author> authorTable = this.GetAuthorTable();

            var matchedAuthor = (from bkAuthor in authorTable
                                 where (bkAuthor.name == author.Name)
                                 select bkAuthor).FirstOrDefault();

            if (matchedAuthor == null)
            {
                try
                {
                    Random data = new Random();

                    DAL.Author newData = new DAL.Author();
                    newData.authorID    = data.Next();  // Dummy init
                    newData.name        = author.Name;
                    newData.description = author.Description;

                    authorTable.InsertOnSubmit(newData);
                    authorTable.Context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }
            else if (matchedAuthor != null)
            {
                try
                {
                    matchedAuthor.name        = author.Name;
                    matchedAuthor.description = author.Description;

                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }

            return("");
        }