コード例 #1
0
        private static void CascadingInsert(BlogDbContext db)
        {
            var post = new Post()
            {
                Title = "New Title 2",
                Body = "New Body.",
                User = db.Users.First(),
                Comments = new Comment[]
                            {
                    new Comment()
                    {
                        Text = "First comment.", Date = DateTime.Now
                    },
                    new Comment()
                    {
                        Text = "Second comment.", Date = DateTime.Now, User = db.Users.First()
                    }
                            },
                Tags = db.Tags.Take(3).ToList(),
                Date = DateTime.Now
            };

            db.Posts.Add(post);
            db.SaveChanges();
        }
コード例 #2
0
        private static void AddPost(BlogDbContext db)
        {
            var post = new Post()
            {
                Title = "New Title",
                Body = "New Body.",
                Date = DateTime.Now
            };

            db.Posts.Add(post);
            db.SaveChanges();

            Console.WriteLine($"Post #{post.ID} created successfully.");
        }