public UserEntity SearchEntity(string key) { Control.CheckName(DomainConstraints.MaxUserName, DomainConstraints.MinUserName, key); using (var context = new MyDBdContext(_connectionString)) { var site = context.SiteEntities.Find(_mySite) ?? throw new InvalidOperationException(); context.Entry(site) .Collection(users => users.Users) .Load(); ; var userEntity = context.UserEntities.Local.SingleOrDefault(user => user.Id.Equals(key)); if (userEntity is null) { return(null); } context.Entry(userEntity) .Reference(session => session.Session) .Load(); context.Entry(userEntity) .Collection(au => au.WinnerAuctionEntities) .Load(); context.Entry(userEntity) .Collection(au => au.SellerAuctionEntities) .Load(); return(userEntity); } }
public AuctionEntity SearchEntity(int key) { using (var context = new MyDBdContext(_connectionString)) { var auction = context.SiteEntities.Single(s => s.Id.Equals(_mySite)) .AuctionEntities.SingleOrDefault(au => au.ID.Equals(key)); if (auction is null) { return(null); } context.Entry(auction).Reference(us => us.Seller).Load(); context.Entry(auction).Reference(au => au.CurrentWinner).Load(); return(auction); } }
public void SaveOnDb(AuctionEntity entity, bool upDate = false) { using (var context = new MyDBdContext(_connectionString)) { if (upDate is false) { context.AuctionEntities.Add(entity); try { context.SaveChanges(); } catch (DbUpdateException exception) //da rivedere lol { throw new NameAlreadyInUseException(entity.Description, "booo", exception.InnerException); } } else { try { context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); } catch (DbUpdateException) { throw new InvalidOperationException(); } } } }
public void SaveOnDb(UserEntity entity, bool upDate = false) { using (var contextDb = new MyDBdContext(_connectionString)) { if (upDate is false) { try { contextDb.UserEntities.Add(entity); contextDb.SaveChanges(); } catch (DbUpdateException exception) //da rivedere lol { var sqlException = exception.GetBaseException() as SqlException; if (sqlException.Number == 2627) { throw new NameAlreadyInUseException(entity.Id); } throw new InvalidOperationException(); } } else { try { contextDb.UserEntities.Attach(entity); contextDb.Entry(entity).Collection(au => au.WinnerAuctionEntities).Load(); if (entity.WinnerAuctionId != null) { var auction = contextDb.AuctionEntities.Find(entity.WinnerAuctionId, _mySite); contextDb.Entry(entity).Collection(au => au.WinnerAuctionEntities).CurrentValue.Remove(auction); entity.WinnerAuctionId = null; contextDb.Entry(auction).State = EntityState.Modified; } contextDb.Entry(entity).State = EntityState.Modified; contextDb.SaveChanges(); } catch (DbUpdateException) { throw new InvalidOperationException(); } } } }
public SessionEntity SearchEntity(string key) { if (key is null) { throw new ArgumentNullException(); } using (var context = new MyDBdContext(_connectionString)) { var session = context.SiteEntities .Where(site => site.Id.Equals(_mySite)) .Select(site => site.SessionEntities) .Single() .SingleOrDefault(sessions => sessions.Id.Equals(key)); if (session is null) { return(null); } context.Entry(session).Reference(us => us.EntityUser).Load(); return(session); } }