예제 #1
0
        private void CheckForVeryLowAvgVote(int id, Vote vote, CodeContext context)
        {
           // var context = new CodeContext();
            var jewel = context.CodeJewels.Include("Votes").FirstOrDefault(j => j.Id == id);
            if (jewel != null)
            {
                double avgVote = 0;
                if (jewel.Votes.Count > 0)
                {
                    avgVote = jewel.Votes.Average(v => v.VoteValue);
                }

                if (avgVote < MINVOTEVALUE)
                {
                    context.Votes.Remove(vote);
                    context.CodeJewels.Remove(jewel);
                    context.SaveChanges();
                }
            }
            else
            {
                throw new InvalidOperationException("Trying to delete, but didn't find codeJewel");
            }

        }
예제 #2
0
        private void ChechForVeryWeekAvgVote(int id, Vote vote, CodeContext context)
        {
            // var context = new CodeContext();
            var jewel = context.CodeJewels.Include("Votes").FirstOrDefault(j => j.Id == id);
            if (jewel != null)
            {
                double avgVote = 0;
                if (jewel.Votes.Count > 0)
                {
                    avgVote = jewel.Votes.Average(v => v.VoteValue);
                }

                if (avgVote < MINVOTEVALUE)
                {
                    context.Votes.Remove(vote);
                    context.CodeJewels.Remove(jewel);
                    context.SaveChanges();
                    //DbEntityEntry entry = context.Entry(vote);
                    //if (entry.State != EntityState.Deleted)
                    //{
                    //    entry.State = EntityState.Deleted;
                    //}
                    //else
                    //{
                    //    context.Votes.Attach(vote);
                    //    context.Votes.Remove(vote);
                    //}

                    //DbEntityEntry entryJewel = context.Entry(jewel);
                    //if (entryJewel.State != EntityState.Deleted)
                    //{
                    //    entryJewel.State = EntityState.Deleted;
                    //}
                    //else
                    //{
                    //    context.CodeJewels.Attach(jewel);
                    //    context.CodeJewels.Remove(jewel);
                    //}

                    //context.SaveChanges();
                }
            }
            else
            {
                throw new InvalidOperationException("Trying to delete, but didn't find codeJewel");
            }
        }