0

Im trying to add some dummy content into my database. I have a sample object called "obj", and I'm using a for loop to insert data like the code below:

  public async Task<Post> Add(Post obj)
        {
            if (db != null)
            {
                obj.Id = 0;
                for (int i = 0; i < 100; i++)
                {
                    await db.Post.AddAsync(obj);
                }
                await db.SaveChangesAsync();

                return obj;
            }

            return null;
        }

However, it does add only 1 record into database, could you please explain what's wrong here?

1
  • You're telling EF a hundred times that obj should be inserted (while one time tends to be enough for the poor thing). Instead different instances should be inserted, so you have to create them in the loop. Commented Aug 12, 2021 at 19:49

3 Answers 3

3

Can you create object of post inside the loop and try again?

for (int i = 0; i < 100; i++)                
{
   Post obj = new Post();
   await db.Post.AddAsync(obj);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your replying, I tried but it didn't work.
1

If you run the below commands before calling saved changes you can see what changes EF Core has queued up.

        var post = new Post();

        for (int i = 0; i < 10; i++)
        {
            
            await db.Posts.AddAsync(dev);
        }

        db.ChangeTracker.DetectChanges();

        Debug.WriteLine(db.ChangeTracker.DebugView.LongView);

        await db.SaveChangesAsync();

This will write to Debug that one post is currently being tracked.

        for (int i = 0; i < 10; i++)
        {
            var post = new Post();
            await db.Posts.AddAsync(dev);
        }

        db.ChangeTracker.DetectChanges();

        Debug.WriteLine(db.ChangeTracker.DebugView.LongView);

        await db.SaveChangesAsync();

This will right to the Debug window the 10 posts are currently being tracked.

In your code when you AddAsync(obj), obj isn't a new object and EF Core is already tracking obj.

Moving Post obj = new Post() into the loop creates a new object for EF Core to track. If these objects aren't being added to the DB my guess would be that they don't meet a requirement for the Post model.

Either way playing around with

db.ChangeTracker.DetectChanges();

Debug.WriteLine(db.ChangeTracker.DebugView.LongView);

should help you be able to track down exactly what's going on.

I do hope you find this information helpful :)

Comments

1

You can try to use AddRange.

public async Task<Post> Add(Post obj)
        {
            if (db != null)
            {
               List<Post>=new List<Post>();
                obj.Id = 0;
                for (int i = 0; i < 100; i++)
                {
                    l.Add(obj);
                }
                await db.Post.AddRangeAsync(l);
                await db.SaveChangesAsync();

                return obj;
            }
            return null;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.