protected void Button1_Click(object sender, EventArgs e) { bool isAlreadyExists; var author = new Author { Name = Name.Text, Surname = Surname.Text }; using (var context = new BookerContext()) { isAlreadyExists = context.Authors.FirstOrDefault(x => x.Name == author.Name && x.Surname == author.Surname) != null; if (!isAlreadyExists) { context.Authors.Add(author); context.SaveChanges(); } } if (isAlreadyExists) { Response.Redirect(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()) + "?authorAction=exist"); } else { Response.Redirect(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()) + "?authorAction=add"); } }
protected void Button1_Click(object sender, EventArgs e) { bool isAlreadyExists; using (var context = new BookerContext()) { var book = new Book { Title = tbTitle.Text, Year = Int32.Parse(tbYear.Text), Author = context.Authors.Find(Int32.Parse(AuthorList.SelectedItem.Value)) }; isAlreadyExists = context.Books.FirstOrDefault(x => x.Title == book.Title && x.Year == book.Year && x.AuthorId == book.AuthorId) != null; if (!isAlreadyExists) { context.Books.Add(book); context.SaveChanges(); } } if (isAlreadyExists) { Response.Redirect(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()) + "?bookAction=exist"); } else { Response.Redirect(Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.Count() - Request.Url.Query.Count()) + "?bookAction=add"); } }