Exemplo n.º 1
0
        public static void UpdatePostsFromJsonWithIdentityResolution(string json)
        {
            using var context = new BlogsContext();

            var posts = JsonConvert.DeserializeObject <List <Post> >(json);

            foreach (var post in posts)
            {
                context.ChangeTracker.TrackGraph(
                    post, node =>
                {
                    var keyValue   = node.Entry.Property("Id").CurrentValue;
                    var entityType = node.Entry.Metadata;

                    var existingEntity = node.Entry.Context.ChangeTracker.Entries()
                                         .FirstOrDefault(
                        e => Equals(e.Metadata, entityType) &&
                        Equals(e.Property("Id").CurrentValue, keyValue));

                    if (existingEntity == null)
                    {
                        Console.WriteLine($"Tracking {entityType.DisplayName()} entity with key value {keyValue}");

                        node.Entry.State = EntityState.Modified;
                    }
                    else
                    {
                        Console.WriteLine($"Discarding duplicate {entityType.DisplayName()} entity with key value {keyValue}");
                    }
                });
            }

            context.SaveChanges();
        }
Exemplo n.º 2
0
        public static void UpdateBlogsFromJson(string json)
        {
            using var context = new BlogsContext();

            var blogs = JsonConvert.DeserializeObject <List <Blog> >(json);

            foreach (var blog in blogs)
            {
                context.Update(blog);
            }

            context.SaveChanges();
        }
Exemplo n.º 3
0
        public static void UpdatePostsFromJson(string json)
        {
            using var context = new BlogsContext();

            var posts = JsonConvert.DeserializeObject <List <Post> >(json);

            foreach (var post in posts)
            {
                context.Update(post);
            }

            context.SaveChanges();
        }
Exemplo n.º 4
0
        public static void PopulateDatabase()
        {
            using var context = new BlogsContext(quiet: true);

            context.AddRange(
                new Blog
            {
                Name    = ".NET Blog",
                Summary = "Posts about .NET",
                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..."
                    },
                },
            },
                new Blog
            {
                Name    = "Visual Studio Blog",
                Summary = "Posts about Visual Studio",
                Posts   =
                {
                    new Post
                    {
                        Title   = "Disassembly improvements for optimized managed debugging",
                        Content =
                            "If you are focused on squeezing out the last bits of performance for your .NET service or..."
                    },
                    new Post
                    {
                        Title   = "Database Profiling with Visual Studio",
                        Content = "Examine when database queries were executed and measure how long the take using..."
                    },
                }
            });

            context.SaveChanges();
        }
Exemplo n.º 5
0
        public static void UpdatePostsFromJsonBad(string json)
        {
            using var context = new BlogsContext();

            var posts = JsonConvert.DeserializeObject <List <Post> >(json);

            try
            {
                foreach (var post in posts)
                {
                    context.Update(post); // Will throw
                }

                context.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine($"{e.GetType().FullName}: {e.Message}");
            }
        }