Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I did not find the exact cause of your issue, but inserts should not be treated as updates. One way to do this is the following: static void InsertPost(Post post) { using var context = new B...
Answer
#2: Post edited
- I did not find the exact cause of your issue, but inserts should not be treated as updates. One way to do this is the following:
- ```c#
- static void InsertPost(Post post)
- {
- using var context = new BloggingContext();
- context.Posts.Add(post);
- context.SaveChanges();
- }
- var newPost = new Post
- {
- BlogId = blog.BlogId,
- Title = "Goodbye World",
- Content = "Some content"
- };
- No need to use the navigation property, since we have the BlogId for the new post.
- ```
- However, it is not recommended for the EF db contexts to leak models like this. As already noticed, it might lead to strange behavior, hard to debug and maintain software.
- I would split all operations into independent "use cases". The code would look like the following:
- ```c#
- public static void Main()
- {
- int blogId = SetupBlog();
- var posts = GetPosts(blogId);
- PrintPosts("Initial posts", posts);
- UpdateFirstPostTitle(blogId);
- PrintPosts("After editing", GetPosts(blogId));
- InsertPost(blogId);
- PrintPosts("After adding a new post", GetPosts(blogId));
- Remove(blogId);
- }
- static void PrintPosts(string header, IEnumerable<Post> posts)
- {
- Console.WriteLine($"\n{header}\n{new string('=', header.Length)}");
- foreach (var post in posts)
- {
- Console.WriteLine($"{post.Title}: {post.Content}");
- }
- }
- static void Remove(int blogId)
- {
- using var context = new BloggingContext();
- var blog = context.Blogs.Find(blogId);
- if (blog != null)
- {
- context.Remove(blog);
- context.SaveChanges();
- }
- }
- static int SetupBlog()
- {
- using var context = new BloggingContext();
- var blog = new Blog { Url = "http://blogs.msdn.com/adonet" };
- blog.Posts.Add(new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
- context.Add(blog);
- context.SaveChanges();
- return blog.BlogId;
- }
- static Post[] GetPosts(int blogId)
- {
- using var context = new BloggingContext();
- return context.Posts.Where(p => p.BlogId == blogId).ToArray();
- }
- static void InsertPost(int blogId)
- {
- using var context = new BloggingContext();
- var newPost = new Post
- {
- BlogId = blogId,
- Title = "Goodbye World",
- Content = "Some content"
- };
- context.Posts.Add(newPost);
- context.SaveChanges();
- }
- static void UpdateFirstPostTitle(int blogId)
- {
- using var context = new BloggingContext();
- var post = context.Posts.First(p => p.BlogId == blogId);
- post.Title = "Hello World (edited)";
- context.SaveChanges();
- }
- ```
Instead of pushing models around, we are working with ids and all the operations are clear and isolated.
- I did not find the exact cause of your issue, but inserts should not be treated as updates. One way to do this is the following:
- ```c#
- static void InsertPost(Post post)
- {
- using var context = new BloggingContext();
- context.Posts.Add(post);
- context.SaveChanges();
- }
- var newPost = new Post
- {
- BlogId = blog.BlogId,
- Title = "Goodbye World",
- Content = "Some content"
- };
- No need to use the navigation property, since we have the BlogId for the new post.
- ```
- However, it is not recommended for the EF db contexts to leak models like this. As already noticed, it might lead to strange behavior, hard to debug and maintain software.
- I would split all operations into independent "use cases". The code would look like the following:
- ```c#
- public static void Main()
- {
- int blogId = SetupBlog();
- var posts = GetPosts(blogId);
- PrintPosts("Initial posts", posts);
- UpdateFirstPostTitle(blogId);
- PrintPosts("After editing", GetPosts(blogId));
- InsertPost(blogId);
- PrintPosts("After adding a new post", GetPosts(blogId));
- Remove(blogId);
- }
- static void PrintPosts(string header, IEnumerable<Post> posts)
- {
- Console.WriteLine($"\n{header}\n{new string('=', header.Length)}");
- foreach (var post in posts)
- {
- Console.WriteLine($"{post.Title}: {post.Content}");
- }
- }
- static void Remove(int blogId)
- {
- using var context = new BloggingContext();
- var blog = context.Blogs.Find(blogId);
- if (blog != null)
- {
- context.Remove(blog);
- context.SaveChanges();
- }
- }
- static int SetupBlog()
- {
- using var context = new BloggingContext();
- var blog = new Blog { Url = "http://blogs.msdn.com/adonet" };
- blog.Posts.Add(new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
- context.Add(blog);
- context.SaveChanges();
- return blog.BlogId;
- }
- static Post[] GetPosts(int blogId)
- {
- using var context = new BloggingContext();
- return context.Posts.Where(p => p.BlogId == blogId).ToArray();
- }
- static void InsertPost(int blogId)
- {
- using var context = new BloggingContext();
- var newPost = new Post
- {
- BlogId = blogId,
- Title = "Goodbye World",
- Content = "Some content"
- };
- context.Posts.Add(newPost);
- context.SaveChanges();
- }
- static void UpdateFirstPostTitle(int blogId)
- {
- using var context = new BloggingContext();
- var post = context.Posts.First(p => p.BlogId == blogId);
- post.Title = "Hello World (edited)";
- context.SaveChanges();
- }
- ```
- Instead of reusing models from other context instances, we are working with ids and all the operations are clear and isolated.
#1: Initial revision
I did not find the exact cause of your issue, but inserts should not be treated as updates. One way to do this is the following: ```c# static void InsertPost(Post post) { using var context = new BloggingContext(); context.Posts.Add(post); context.SaveChanges(); } var newPost = new Post { BlogId = blog.BlogId, Title = "Goodbye World", Content = "Some content" }; No need to use the navigation property, since we have the BlogId for the new post. ``` However, it is not recommended for the EF db contexts to leak models like this. As already noticed, it might lead to strange behavior, hard to debug and maintain software. I would split all operations into independent "use cases". The code would look like the following: ```c# public static void Main() { int blogId = SetupBlog(); var posts = GetPosts(blogId); PrintPosts("Initial posts", posts); UpdateFirstPostTitle(blogId); PrintPosts("After editing", GetPosts(blogId)); InsertPost(blogId); PrintPosts("After adding a new post", GetPosts(blogId)); Remove(blogId); } static void PrintPosts(string header, IEnumerable<Post> posts) { Console.WriteLine($"\n{header}\n{new string('=', header.Length)}"); foreach (var post in posts) { Console.WriteLine($"{post.Title}: {post.Content}"); } } static void Remove(int blogId) { using var context = new BloggingContext(); var blog = context.Blogs.Find(blogId); if (blog != null) { context.Remove(blog); context.SaveChanges(); } } static int SetupBlog() { using var context = new BloggingContext(); var blog = new Blog { Url = "http://blogs.msdn.com/adonet" }; blog.Posts.Add(new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" }); context.Add(blog); context.SaveChanges(); return blog.BlogId; } static Post[] GetPosts(int blogId) { using var context = new BloggingContext(); return context.Posts.Where(p => p.BlogId == blogId).ToArray(); } static void InsertPost(int blogId) { using var context = new BloggingContext(); var newPost = new Post { BlogId = blogId, Title = "Goodbye World", Content = "Some content" }; context.Posts.Add(newPost); context.SaveChanges(); } static void UpdateFirstPostTitle(int blogId) { using var context = new BloggingContext(); var post = context.Posts.First(p => p.BlogId == blogId); post.Title = "Hello World (edited)"; context.SaveChanges(); } ``` Instead of pushing models around, we are working with ids and all the operations are clear and isolated.