Example #1
0
 static void DeleteNote()
 {
     using (var dbContext = new NoteDbContext())
     {
         var note = dbContext.Notes.Find(2);
         dbContext.Notes.Remove(note);
         dbContext.SaveChanges();
     }
 }
Example #2
0
        static void UpdateNote(int noteId)
        {
            Console.WriteLine("Before updating");
            GetNote(noteId);

            using (var dbContext = new NoteDbContext())
            {
                var note = dbContext.Notes.Find(noteId);
                note.Title      = "EF configuration Tips";
                note.UpdatedUtc = DateTime.UtcNow;
                dbContext.SaveChanges();
            }

            Console.WriteLine("After updating");
            GetNote(noteId);
        }
Example #3
0
        static void CreateNote()
        {
            //Insert a new record with ORM (Entity Framework)
            using (var dbContext = new NoteDbContext())
            {
                // Create notebook
                var notebook = new Notebook()
                {
                    Name = "Programming Tips"
                };
                dbContext.Notebooks.Add(notebook);

                // Create tag
                var tag = new Tag
                {
                    Name = "ef"
                };
                dbContext.Tags.Add(tag);

                // Crate note
                var note = new Note()
                {
                    Title   = "EF Tips",
                    Content = "How to create entity classes from an existing database...",
                    //CreatedUtc = DateTime.UtcNow,
                    Notebook = notebook
                };
                note.Tags.Add(tag);

                dbContext.Notes.Add(note);

                dbContext.SaveChanges();
                Console.WriteLine($"New notebook inserted with Id {notebook.Id}");
                Console.WriteLine($"New tag inserted with Id {tag.Id}");
                Console.WriteLine($"New note inserted with Id {note.Id}");
            }
        }