private static void CascadingInsert() { BlogDBContext db = new BlogDBContext(); Post post = new Post() { Title = "New Post Title", Date = DateTime.Now, Body = "This post has comments and tags", User = db.Users.First(), Comments = new Comment[] { new Comment() { Text = "Comment 1", Date = DateTime.Now }, new Comment() { Text = "Comment 2", Date = DateTime.Now, User = db.Users.First() } }, Tags = db.Tags.Take(3).ToList() }; db.Posts.Add(post); db.SaveChanges(); }
private static void UpdateExistingData() { BlogDBContext db = new BlogDBContext(); User user = db.Users .Where(u => u.Username == "maria") .First(); user.PasswordHash = Guid.NewGuid().ToByteArray(); db.SaveChanges(); Console.WriteLine("User #{0} ({1}) has a new random password.", user.ID, user.Username); }
private static void DeleteExistingData() { BlogDBContext db = new BlogDBContext(); Post lastPost = db.Posts .OrderByDescending(p => p.ID) .First(); db.Comments.RemoveRange(lastPost.Comments); lastPost.Tags.Clear(); db.Posts.Remove(lastPost); db.SaveChanges(); Console.WriteLine($"Deleted post #{lastPost.ID}"); }
private static void CreateNewData() { BlogDBContext db = new BlogDBContext(); Post post = new Post() { Title = "New Title", Body = "New Post Body", Date = DateTime.Now }; db.Posts.Add(post); db.SaveChanges(); Console.WriteLine("Post #{0} created.", post.ID); }