コード例 #1
0
 private static void UpdateBlogAndTheirPost()
 {
     using (var db = new CtContext())
     {
         var blogs = db.Blogs.Include(x => x.Posts).ToList();
         blogs.ForEach(x =>
         {
             x.Rating = 170;
             x.Posts.ForEach(y =>
             {
                 y.Content = y.Content + DateTime.Now.ToString();
             });
             db.Entry(x).State = EntityState.Modified;
         });
         db.SaveChanges();
     }
 }
コード例 #2
0
 private static void AddEntity()
 {
     using (var db = new CtContext())
     {
         if (!db.Blogs.Any())
         {
             db.Blogs.Add(new Blog
             {
                 Url    = "http://sample.com",
                 Rating = 5,
                 Posts  = new List <Post> {
                     new Post {
                         Content = "1",
                         Title   = "1"
                     }
                 }
             });
             db.SaveChanges();
         }
     }
 }