public Auction CreateAuction(DateTime date, string description, List<Art> arts) { Auction auction = new Auction() { Date = date, Description = description, Arts = arts }; using (AuctionDBContext db = new AuctionDBContext()) { //db.Arts.Load(); foreach (var art in arts) { db.Arts.Attach(art); db.Entry(art).Entity.Auction = auction; db.Entry(art).State = EntityState.Modified; // //db.Entry(art).State = EntityState.Modified;; } db.Auktions.Add(auction); db.DebugDetectChanges(); db.SaveChanges(); } return auction; }
private void btnCancel_Click(object sender, EventArgs e) { getArtData(); getUpcomingAuctions(); getEndedAuction(); grbCreate.Text = "Opret"; dtpDate.Value = DateTime.Now; txtDecs.Text = ""; lbxSelectedArt.DataSource = null; auction = null; }
public Budgivning(Auction au) { InitializeComponent(); lblHeader.Text = "Auktion den " + au.Date; currentAuction = au; ctrArt = new CtrArt(); ctrAuc = new CtrAuction(); dgvArts.DataSource = ctrArt.RetrieveAll(au.Id); }
public void DeleteAuction(Auction auction) { if (auction == null) throw new NullReferenceException("auction"); using (AuctionDBContext db = new AuctionDBContext()) { db.Auktions.Attach(auction); db.Entry(auction).State = EntityState.Deleted; db.Auktions.Remove(auction); db.SaveChanges(); } }
private void lbxUpcoming_MouseClick(object sender, MouseEventArgs e) { auction = lbxUpcoming.SelectedItem as Auction; dtpDate.Value = auction.Date; txtDecs.Text = auction.Description; lbxSelectedArt.DataSource = auction.Arts; grbCreate.Text = "Rediger"; }
public Auction UpdateAuction(Auction auction) { if (auction == null) throw new NullReferenceException("auction"); using (AuctionDBContext db = new AuctionDBContext()) { try { var artsInDb = db.Arts.Where(x => x.AuctionId == auction.Id).ToList(); foreach (var art in artsInDb) { art.AuctionId = null; db.Entry(art).State = EntityState.Modified; } var artsInDbIds = artsInDb.Select(x => x.Id); foreach (var art in auction.Arts) { art.AuctionId = auction.Id; if (!artsInDbIds.Contains(art.Id)) { db.Entry(art).State = EntityState.Modified; } else { var tempObj = db.ChangeTracker.Entries<Art>().Single(x => x.Entity.Id == art.Id); tempObj.Entity.AuctionId = auction.Id; } } var artsList = auction.Arts; auction.Arts = null; db.Entry(auction).State = EntityState.Modified; db.DebugDetectChanges(); db.SaveChanges(); auction.Arts = artsList; } catch (UpdateException) { } } return auction; }
public Art UpdateToAuction(int id, Auction au) { using (AuctionDBContext db = new AuctionDBContext()) { Art aTemp = db.Arts.SingleOrDefault(x => x.Id == id); aTemp.Auction = au; try { db.Arts.Attach(aTemp); var entry = db.Entry(aTemp); entry.State = EntityState.Modified; db.SaveChanges(); } catch (Exception e) { throw e; } return RetrieveById(id); } }