示例#1
0
            public static void PopulateDatabase(OptionalBlogsContext context)
            {
                using (context)
                {
                    context.Add(
                        new Blog
                    {
                        Name  = ".NET Blog",
                        Posts =
                        {
                            new Post
                            {
                                Title   = "Announcing the Release of EF Core 5.0",
                                Content = "Announcing the release of EF Core 5.0, a full featured cross-platform..."
                            },
                            new Post
                            {
                                Title   = "Announcing F# 5",
                                Content = "F# 5 is the latest version of F#, the functional programming language..."
                            },
                        }
                    });

                    context.SaveChanges();
                }
            }
示例#2
0
 public static void RecreateCleanDatabase(OptionalBlogsContext context)
 {
     using (context)
     {
         context.Database.EnsureDeleted();
         context.Database.EnsureCreated();
     }
 }
示例#3
0
            public static Dictionary <DeleteBehavior, string> GatherData(Action <OptionalBlogsContext> action)
            {
                var results = new Dictionary <DeleteBehavior, string>();

                foreach (var deleteBehavior in DeleteBehaviors)
                {
                    RecreateCleanDatabase(new OptionalBlogsContext(deleteBehavior));
                    PopulateDatabase(new OptionalBlogsContext(deleteBehavior));

                    try
                    {
                        using var context = new OptionalBlogsContext(deleteBehavior);

                        action(context);

                        context.ChangeTracker.DetectChanges();

                        var deletingPosts    = context.ChangeTracker.Entries <Post>().Any(e => e.State == EntityState.Deleted);
                        var settingFksToNull = context.ChangeTracker.Entries <Post>().Any(e => e.State == EntityState.Modified);

                        context.SaveChanges();

                        var deletedPosts = !context.Posts.AsNoTracking().Any();

                        results[deleteBehavior] =
                            deletingPosts
                                ? "Dependents deleted by EF Core"
                                : deletedPosts
                                    ? "Dependents deleted by database"
                                    : settingFksToNull
                                        ? "Dependent FKs set to null by EF Core"
                                        : "Dependent FKs set to null by database";
                    }
                    catch (Exception e)
                    {
                        results[deleteBehavior] = $"`{e.GetType().Name}`";
                    }
                }

                return(results);
            }