Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

60%
+1 −0
Q&A Updating the database reverses previous changes

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...

posted 1y ago by Alexei‭  ·  edited 1y ago by Alexei‭

Answer
#2: Post edited by user avatar Alexei‭ · 2023-04-14T08:22:03Z (over 1 year ago)
minor fixes
  • 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 by user avatar Alexei‭ · 2023-04-14T08:20:02Z (over 1 year ago)
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.