Friends, I'm having trouble iterating the results and saving them to the database. I have this method below that correctly runs through the amount of selected services and saves it in a table of financial items. However, it saves only the last service performed.
.
.
FinancialItems financialitems = new FinancialItems();
for (int i = 0; i < services.Count; i++)
{
financialitems .Cod= services[i].Cod;
financialitems .Quantity= 1;
financialitems .Price = services.Price;
financialitems .Financeiro = financial;
}
_db.FinancialItems.Add(financialitems );
_db.SaveChanges();
How can I reorganize this code so that, if services.cout is equal to 3, save the 3 records in the database?
Thanks for the feedback. I made the changes. However, I realized that only one item is saved and the "Cod" field
(financialitems.Cod = services[i].Cod) is updated with each iteration.
When I use it this way, it gives an error: "duplicate entry". Is searching for the last record saved in the bank and adding 1 a good practice?
FinancialItems financialitems = new FinancialItems();
for (int i = 0; i < services.Count; i++)
{
financialitems .Cod= services[i].Cod;
financialitems .Quantity= 1;
financialitems .Price = services.Price;
financialitems .Financeiro = financial;
_db.FinancialItems.Add(financialitems );
_db.SaveChanges();
}
The code you've shared is very difficult to understand. IMHO, the code has design errors but we cannot see enough of the code to figure out what you are trying to do.
Thanks for the feedback. I made the changes. However, I realized that only one item is saved and the "Cod" field
(financialitems.Cod = services[i].Cod) is updated with each iteration.
When I use it this way, it gives an error: "duplicate entry". Is searching for the last record saved in the bank and adding 1 a good practice?
FinancialItems financialitems = new FinancialItems();
for (int i = 0; i < services.Count; i++)
{
financialitems .Cod= services[i].Cod;
financialitems .Quantity= 1;
financialitems .Price = services.Price;
financialitems .Financeiro = financial;
_db.FinancialItems.Add(financialitems );
_db.SaveChanges();
}
just another coding error. you only create one entity object and add it several times. you need to create a new object in the loop. the save changes should be moved outside.
for (int i = 0; i < services.Count; i++)
{
_db.FinancialItems.Add(new FinancialItems
{
Cod = services[i].Cod,
Quantity = 1,
Price = services.Price, //?? why no index?
Financeiro = financial //?> why is this the same value for all?
}
}
_db.SaveChanges();
I changed the code, but only the last record is saved. I also tried to move the _db.SaveChanges() part, but it just updates the service ID. Only one iteration is saved in the database.
I changed the code, but only the last record is saved. I also tried to move the _db.SaveChanges() part, but it just updates the service ID. Only one iteration is saved in the database.
</div> <div></div> <div>I'm pretty sure the issue you are facing has more to do with the design than Entity Framework. Your code has several unknowns which make understanding the intent difficult. What you have shown does not look
right.</div>
Member
2 Points
9 Posts
Problems when iterating results and saving to the database
Apr 06, 2021 07:58 PM|ebiagi|LINK
Friends, I'm having trouble iterating the results and saving them to the database. I have this method below that correctly runs through the amount of selected services and saves it in a table of financial items. However, it saves only the last service performed.
How can I reorganize this code so that, if services.cout is equal to 3, save the 3 records in the database?
Thanks for any comments!
All-Star
53661 Points
24018 Posts
Re: Problems when iterating results and saving to the database
Apr 06, 2021 08:16 PM|mgebhard|LINK
Move the Add and Save inside the loop.
Member
2 Points
9 Posts
Re: Problems when iterating results and saving to the database
Apr 06, 2021 08:57 PM|ebiagi|LINK
Thanks for the feedback. I made the changes. However, I realized that only one item is saved and the "Cod" field (financialitems.Cod = services[i].Cod) is updated with each iteration.
When I use it this way, it gives an error: "duplicate entry". Is searching for the last record saved in the bank and adding 1 a good practice?
All-Star
53661 Points
24018 Posts
Re: Problems when iterating results and saving to the database
Apr 06, 2021 09:12 PM|mgebhard|LINK
The code you've shared is very difficult to understand. IMHO, the code has design errors but we cannot see enough of the code to figure out what you are trying to do.
All-Star
58444 Points
15778 Posts
Re: Problems when iterating results and saving to the database
Apr 06, 2021 09:41 PM|bruce (sqlwork.com)|LINK
just another coding error. you only create one entity object and add it several times. you need to create a new object in the loop. the save changes should be moved outside.
Member
2 Points
9 Posts
Re: Problems when iterating results and saving to the database
Apr 07, 2021 01:42 PM|ebiagi|LINK
I changed the code, but only the last record is saved. I also tried to move the _db.SaveChanges() part, but it just updates the service ID. Only one iteration is saved in the database.
All-Star
53661 Points
24018 Posts
Re: Problems when iterating results and saving to the database
Apr 07, 2021 02:10 PM|mgebhard|LINK
Member
2 Points
9 Posts
Re: Problems when iterating results and saving to the database
Apr 08, 2021 02:03 PM|ebiagi|LINK
Thank you all for the comments!
The solution was to use AddRange and organize the code a little more!