protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Beer b = new PremiumBeer(); b.Name = "Primator"; b.Weight = b.InitialWeight = 645; b.Country = "CZECH"; using (TweetBeerContainer dbContainer = new TweetBeerContainer()) { #region Inserting dbContainer.AddToBeer(b); dbContainer.SaveChanges(); #endregion #region Updating b.Name = "Corona"; dbContainer.SaveChanges(); #endregion #region Delete //dbContainer.DeleteObject(b); //dbContainer.SaveChanges(); #endregion #region LINQ Query var coronas = from beer in dbContainer.Beer.ToList() where beer.Name == "Corona" select beer; #endregion } #region Equivalent to using //TweetBeerContainer v = null; //try //{ // v = new TweetBeerContainer(); // // Fazer um monte de coisa //} //catch //{ //} //finally //{ // v.Dispose(); //} #endregion } }
protected void btnAddToFavorites_Click(object sender, EventArgs e) { string v = ViewState["S"] as string; ListItem selectedBeer = this.ucBeerList.SelectedBeer; if (selectedBeer != null) { this.lbFavorites.Items.Add(new ListItem(selectedBeer.Text)); #region Adding in Database using (TweetBeerContainer tweetBeerContainer = new TweetBeerContainer()) { long id = Int64.Parse(selectedBeer.Value); Beer currentBeer = tweetBeerContainer.Beer .Single(b => b.Id == id); FavoriteBeer favoriteBeer = new FavoriteBeer(); favoriteBeer.Beer = currentBeer; favoriteBeer.CreationDate = DateTime.Now; favoriteBeer.User = Session["userName"] as string; tweetBeerContainer.AddToFavoriteBeerSet(favoriteBeer); tweetBeerContainer.SaveChanges(); // Twitter currentBeer.Tweet(String.Format("{0} added to favorites by {1}", currentBeer.Name, favoriteBeer.User )); } #endregion } // Cleaning up Beer List this.ucBeerList.RemoveSelectedBeer(); }