1

I am having trouble when inserting multiple records. When I try to insert 2 records the debugger shows the count of 2 but it only saves 1 record.

This is my code in my manager class

public void Create(IEnumerable<CollectionModel> p )
{
    collection module = new collection();
    foreach (var asa in p)
    {               
        module.AccountId = 1;
        module.Amount = asa.Amount;
        module.NameSou = asa.NameSou;
        module.Date = asa.Date;
        module.CreatedDatetime = DateTime.Now;
        module.UpdatedDatetime = DateTime.Now;

        _context.collection.Add(module);
    }
    _context.SaveChanges();
}
2
  • Try adding _context.collection.Add(module); inside for loop Commented Mar 19, 2019 at 3:32
  • thank you for your help. I just edited my question. Its still saved 1 data Commented Mar 19, 2019 at 3:36

2 Answers 2

1

Small Correction in your code. You are creating one module and updating the same every time. So it will always save the list of last item in your case list of last item of p.

 public void Create(IEnumerable<CollectionModel> p )
 {
   foreach (var asa in p)
     {   
       collection module = new collection();            
       module.AccountId = 1;
       module.Amount = asa.Amount;
       module.NameSou = asa.NameSou;
       module.Date = asa.Date;
       module.CreatedDatetime = DateTime.Now;
       module.UpdatedDatetime = DateTime.Now;

      _context.collection.Add(module);
    }
   _context.SaveChanges();
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Do i need to do a List or IEnumerable of Collection Module?
Sorry I din't get your question?
Do i need to make a ` List<collection> module = new List<collection>();` instead of collection module = new collection();?
No not list of collection just new collection, as foreach loop iterates every time, it will create a new instance of collection and it will be added to context collection.
1

Entity Framework offers two methods to insert records into database. Add() method allows to insert a single entity. AddRange() method inserts an IEnumerable collection. You can find more information from here.

Example:

 public void Create(IEnumerable<CollectionModel> args )
 {
    var modules = args.Select(asa=> new collection{
       AccountId = 1,
       Amount = asa.Amount,
       NameSou = asa.NameSou,
       Date = asa.Date,
       CreatedDatetime = DateTime.Now,
       UpdatedDatetime = DateTime.Now
      }).ToList(); 
    _context.collection.AddRange(modules);
   _context.SaveChanges();
}

5 Comments

Is there any exception or the same result?
It works! Thank you! I just remove all the semi colon :) Thank you
hi @Ashiquzzaman just one more thine, if i want to add some data to other table, i will just do the same? right?
@PaulVincentDoroyan Yes. You can do. and one more suggestion please spend some time to read my provided link.
im getting 500 internal server error when I try to save 2 datas in two tables

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.