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 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.º 3
0
        private static void RetrievingDataGraphs()
        {
            // Fetch a Topic Record and all related Comments to that Topic

            Topic eagerLoadedTopic;
            using (var context = new DiscussionBoardContext())
            {
                // Example of Eager Loading
                eagerLoadedTopic = context.Topics.Where(t => t.TopicName
                    .Contains("Will Mr.Gates ever"))
                    .Include(t => t.Comments)
                    .FirstOrDefault();
            }

            Topic explicitLoadedTopic;
            using (var context = new DiscussionBoardContext())
            {
                // Example of Explicit Loading
                explicitLoadedTopic = context.Topics.Where(t => t.TopicName
                    .Contains("Will Mr.Gates ever"))
                    .FirstOrDefault();

                // Explicit Loading done here
                context.Entry(explicitLoadedTopic)
                       .Collection(t => t.Comments)
                       .Load();
            }
        }