Ejemplo n.º 1
0
        public static void Run()
        {
            using (var db = new BloggingContext())
            {
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();

                db.Blogs.Add(new Blog
                {
                    Url = "http://sample.com",
                    Posts = new List<Post>
                    {
                        new Post { Title = "Saving Data with EF" },
                        new Post { Title = "Cascade Delete with EF" }
                    }
                });

                db.SaveChanges();
            }

            using (var db = new BloggingContext())
            {
                var blog = db.Blogs.Include(b => b.Posts).First();
                db.Remove(blog);
                db.SaveChanges();
            }

            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog
                {
                    Url = "http://sample.com",
                    Posts = new List<Post>
                    {
                        new Post { Title = "Saving Data with EF" },
                        new Post { Title = "Cascade Delete with EF" }
                    }
                });

                db.SaveChanges();
            }

            using (var db = new BloggingContext())
            {
                var blog = db.Blogs.First();
                db.Remove(blog);
                db.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        private static void DeleteOrphansSample(DeleteBehavior deleteBehavior, bool requiredRelationship)
        {
            Console.WriteLine(
                $"Test deleting orphans with DeleteBehavior.{deleteBehavior} and {(requiredRelationship ? "a required" : "an optional")} relationship:");

            InitializeDatabase(requiredRelationship);

            using var context = new BloggingContext(deleteBehavior, requiredRelationship);

            #region DeleteOrphansVariations
            var blog  = context.Blogs.Include(b => b.Posts).First();
            var posts = blog.Posts.AsQueryable().ToList();

            DumpEntities("  After loading entities:", context, blog, posts);

            blog.Posts.Clear();

            DumpEntities("  After making posts orphans:", context, blog, posts);

            try
            {
                Console.WriteLine();
                Console.WriteLine("  Saving changes:");

                context.SaveChanges();

                DumpSql();

                DumpEntities("  After SaveChanges:", context, blog, posts);
            }
            catch (Exception e)
            {
                DumpSql();

                Console.WriteLine();
                Console.WriteLine(
                    $"  SaveChanges threw {e.GetType().Name}: {(e is DbUpdateException ? e.InnerException.Message : e.Message)}");
            }
            #endregion

            Console.WriteLine();
        }
Ejemplo n.º 3
0
        private static void DeleteBehaviorSample(DeleteBehavior deleteBehavior, bool requiredRelationship)
        {
            Console.WriteLine(
                $"Test using DeleteBehavior.{deleteBehavior} with {(requiredRelationship ? "required" : "optional")} relationship:");

            InitializeDatabase(requiredRelationship);

            using var context = new BloggingContext(deleteBehavior, requiredRelationship);

            #region DeleteBehaviorVariations
            var blog  = context.Blogs.Include(b => b.Posts).First();
            var posts = blog.Posts.ToList();

            DumpEntities("  After loading entities:", context, blog, posts);

            context.Remove(blog);

            DumpEntities($"  After deleting blog '{blog.BlogId}':", context, blog, posts);

            try
            {
                Console.WriteLine();
                Console.WriteLine("  Saving changes:");

                context.SaveChanges();

                DumpSql();

                DumpEntities("  After SaveChanges:", context, blog, posts);
            }
            catch (Exception e)
            {
                DumpSql();

                Console.WriteLine();
                Console.WriteLine(
                    $"  SaveChanges threw {e.GetType().Name}: {(e is DbUpdateException ? e.InnerException.Message : e.Message)}");
            }
            #endregion

            Console.WriteLine();
        }
Ejemplo n.º 4
0
        private static void InitializeDatabase(bool requiredRelationship)
        {
            using var context = new BloggingContext(DeleteBehavior.ClientSetNull, requiredRelationship);
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.Blogs.Add(
                new Blog
            {
                Url   = "http://sample.com",
                Posts = new List <Post> {
                    new Post {
                        Title = "Saving Data with EF"
                    }, new Post {
                        Title = "Cascade Delete with EF"
                    }
                }
            });

            context.SaveChanges();
        }