Пример #1
0
        public static void InsertOrUpdateGraph(BloggingContext context, Blog blog)
        {
            var existingBlog = context.Blogs
                               .Include(b => b.Posts)
                               .FirstOrDefault(b => b.BlogId == blog.BlogId);

            if (existingBlog == null)
            {
                context.Add(blog);
            }
            else
            {
                context.Entry(existingBlog).CurrentValues.SetValues(blog);
                foreach (var post in blog.Posts)
                {
                    var existingPost = existingBlog.Posts
                                       .FirstOrDefault(p => p.PostId == post.PostId);

                    if (existingPost == null)
                    {
                        existingBlog.Posts.Add(post);
                    }
                    else
                    {
                        context.Entry(existingPost).CurrentValues.SetValues(post);
                    }
                }
            }

            context.SaveChanges();
        }
Пример #2
0
        public static async Task InsertOrUpdateGraphAsync(BloggingContext context, Blog blog)
        {
            var existingBlog = await context.Blogs
                               .Include(b => b.Posts)
                               .FirstOrDefaultAsync(b => b.BlogId == blog.BlogId);

            if (existingBlog == null)
            {
                context.Add(blog);
            }
            else
            {
                context.Entry(existingBlog).CurrentValues.SetValues(blog);
                foreach (var post in blog.Posts)
                {
                    var existingPost = await existingBlog.Posts
                                       .AsQueryable()
                                       .FirstOrDefaultAsync(p => p.PostId == post.PostId);

                    if (existingPost == null)
                    {
                        existingBlog.Posts.Add(post);
                    }
                    else
                    {
                        context.Entry(existingPost).CurrentValues.SetValues(post);
                    }
                }
            }

            await context.SaveChangesAsync();
        }
Пример #3
0
        public static void InsertOrUpdate(BloggingContext context, Blog blog)
        {
            var existingBlog = context.Blogs.Find(blog.BlogId);

            if (existingBlog == null)
            {
                context.Add(blog);
            }
            else
            {
                context.Entry(existingBlog).CurrentValues.SetValues(blog);
            }

            context.SaveChanges();
        }
Пример #4
0
        public static async Task InsertOrUpdateAsync(BloggingContext context, Blog blog)
        {
            var existingBlog = await context.Blogs.FindAsync(blog.BlogId);

            if (existingBlog == null)
            {
                context.Add(blog);
            }
            else
            {
                context.Entry(existingBlog).CurrentValues.SetValues(blog);
            }

            await context.SaveChangesAsync();
        }