Ejemplo n.º 1
0
        private static void DeleteRandomComment()
        {
            // Fetching a Random Comment
            Comment comment;
            using (var context = new DiscussionBoardContext())
            {
                // Get me a random comment
                comment = context.Comments.FirstOrDefault();
            }

            using (var context = new DiscussionBoardContext())
            {
                context.Database.Log = Console.WriteLine;

                // Note*
                // We need to apprise this Context of the changes we would
                // like to be made in the Database to the
                // fetched Comment Object!

                // Marking the object as Deleted for the Context to
                // take action and fire a Delete Statement for the relevant
                // Comment Object.
                context.Entry(comment).State = EntityState.Deleted;

                // Save the Updated Comment Object
                context.SaveChanges();
                Console.ReadKey();
            }
        }
Ejemplo n.º 2
0
        private static void InsertCatergories()
        {
            using (var context = new DiscussionBoardContext())
            {
                IList<Category> _categories;
                _categories = context
                    .Categories
                    .Where(c =>
                           c.Name == DiscussionCategories.Science
                        || c.Name == DiscussionCategories.Sports
                        || c.Name == DiscussionCategories.Technology
                        || c.Name == DiscussionCategories.Politics
                        || c.Name == DiscussionCategories.Philosophy
                        ).ToList();

                if (_categories.Count > 0)
                {
                    // Do Nothing.
                }
                else
                {
                    // No Records Found - Add Them.
                    IList<Category> categories = _getInitialCategories();
                    foreach (var definedCategory in categories)
                    {
                        context.Database.Log = Console.WriteLine;
                        context.Categories.Add(definedCategory);
                        context.SaveChanges();

                        // To read the Log File in Console in Detail
                        Console.ReadKey();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static void UpdateRandomCommentInDisconnectedState()
        {
            // Fetching a Random Comment
            Comment comment;
            using (var context = new DiscussionBoardContext())
            {
                // Get me a random comment
                comment = context.Comments.FirstOrDefault();
            }

            // Update the data now
            // This will mimick us fetching data and passing
            // it to some sort of client - example a Web page
            // for update.

            // Mimicking Update the Web Client will do
            comment.CommentDetail = "Comment Updated by: "+
                "UpdateRandomCommentInDisconnectedState()" +
                    " Method on DateTime" + System.DateTime.Now.ToString();

            // Now, the data HashSet to TransactionalBehavior saved.

            using (var context = new DiscussionBoardContext())
            {
                context.Database.Log = Console.WriteLine;

                // Note* This Context has NO IDEA of what has happened to the
                // above Comment Object - It has no Knowledge that it has been
                // Updated!

                // We need to apprise it of the changes we have made to the
                // Comment Object!

                // Marking the object as Updated/Modified for the Context to
                // take action and fire a Update Statement for the relevant
                // Comment Object.
                context.Entry(comment).State = EntityState.Modified;

                // Save the Updated Comment Object
                context.SaveChanges();
                Console.ReadKey();
            }
        }
Ejemplo n.º 4
0
        private static void UpdateRandomComment()
        {
            // Fetching a Random Comment
            Comment comment;
            using (var context = new DiscussionBoardContext())
            {
                context.Database.Log = Console.WriteLine;

                // Get me a random comment
                comment = context.Comments.FirstOrDefault();

                // Lets make some changes
                comment.CommentDetail = "Comment Updated by: UpdateRandomComment()" +
                    " Method on DateTime" + System.DateTime.Now.ToString();

                // Save the Updated Comment Object
                context.SaveChanges();
                Console.ReadKey();
            }
        }
Ejemplo n.º 5
0
        private static void InsertTopics()
        {
            // Fetching Categories First: Technology and Sports.
            IList<Category> categories;
            using (var context = new DiscussionBoardContext())
            {
                categories = context.Categories
                                    .Where(c =>
                                        c.Name == DiscussionCategories.Technology
                                        || c.Name == DiscussionCategories.Sports)
                                    .ToList();
            }
            // Time to Create a Topic which will fall under The above 2 Categories
            Topic topic = new Topic
            {
                StartDate = System.DateTime.Now,
                IsActive = true,
                TopicName = "Will Mr.Gates ever buy a Basketball Team?"
            };
            foreach (var _category in categories)
            {
                var categoryRec = new Category
                {
                    CategoryId = _category.CategoryId,
                    Name = _category.Name
                };
                topic.Categories.Add(categoryRec);
            }
            // Saving New Topic
            using (var context = new DiscussionBoardContext())
            {
                context.Database.Log = Console.WriteLine;

                // Everything in the Graph will be marked as Added Now!
                // Including the Category Records...
                context.Topics.Add(topic);

                // Marking Category Records as UnChanged - No Insert required!
                foreach (var entry in context.ChangeTracker.Entries<Category>())
                {
                    entry.State = EntityState.Unchanged;
                }

                context.SaveChanges();
                Console.ReadKey();
            }
        }
Ejemplo n.º 6
0
        private static void InsertMembers()
        {
            IList<Member> members = new List<Member>();
            for (int i=1; i<= 5; i++)
            {
                var member = new Member
                {
                    FirstName = "Member" + i,
                    LastName = "SomeLastName" + i,

                    // BOTS have just been born.. RUN...
                    DateOfBirth = System.DateTime.Now,

                    // Adding ContactDetaiul record on the FLY!...
                    ContactDetail = new ContactDetail
                    {
                        MobilePhone = "MobileNumber" + i,
                        TwitterAlias = "TwitterAlias" + i,
                        Facebook = "Facebook" + i,
                        LinkedIn = "Linkedin" + i
                    }
                };
                members.Add(member);
            }
            // Saving New Members
            using (var context = new DiscussionBoardContext())
            {
                context.Database.Log = Console.WriteLine;

                // Everything in the Graph will be marked as Added Now!
                // Including the ContactDetail Records...
                context.Members.AddRange(members);

                context.SaveChanges();
                Console.ReadKey();
            }
        }
Ejemplo n.º 7
0
        private static void InsertInterests()
        {
            IList<Interest> interests = new List<Interest>();
            // Fetching Members...
            IList<Member> members;
            using (var context = new DiscussionBoardContext())
            {
                members = context.Members
                                    .Where(m => m.FirstName
                                        .Contains("Member")).ToList();
            }
            // Time to Create Interests for Members
            int enumCounter = 1;
            foreach(var member in members)
            {
                var interest = new Interest
                {
                    // the power of Enums!
                    InterestName = (DiscussionCategories) enumCounter,
                    // Use Foreign Key to indicate this is not a Insert!
                    MemberId = member.MemberId
                };
                enumCounter++;
                interests.Add(interest);
            }
            // Saving New Interests
            using (var context = new DiscussionBoardContext())
            {
                context.Database.Log = Console.WriteLine;

                // Everything in the Graph will be marked as Added Now!
                context.Interests.AddRange(interests);

                context.SaveChanges();
                Console.ReadKey();
            }
        }
Ejemplo n.º 8
0
        private static void InsertComments()
        {
            IList<Comment> comments = new List<Comment>();
            // Fetching Members...
            IList<Member> members;
            using (var context = new DiscussionBoardContext())
            {
                members = context.Members
                                    .Where(m => m.FirstName
                                        .Contains("Member")).ToList();
            }
            //Fetching Topic
            Topic topic;
            using (var context = new DiscussionBoardContext())
            {
                topic = context.Topics.Where(t => t.TopicName
                    .Contains("Will Mr.Gates ever")).FirstOrDefault();
            }
            // Time to Create Comments for Members
            foreach (var member in members)
            {
                var comment = new Comment
                {
                    CommentDetail = "The Comment is:" +
                         " from: " + member.FirstName + " " + member.LastName,

                    // Use Foreign Key to indicate this is not a Insert!
                    MemberId = member.MemberId,
                    // Use Foreign Key to indicate this is not a Insert!
                    TopicId = topic.TopicId,

                    CreatedDateTime = System.DateTime.Now,
                    NumberOfLikes = 0
                };
                comments.Add(comment);
            }
            // Saving New Comments
            using (var context = new DiscussionBoardContext())
            {
                context.Database.Log = Console.WriteLine;

                // Everything in the Graph will be marked as Added Now!
                context.Comments.AddRange(comments);

                context.SaveChanges();
                Console.ReadKey();
            }
        }