It is necessary to add 500 000 lines in the table. But my version of the code takes too much time. Table Question 5 fields: id, NameQuestion, DetailsQuestion, UserId (Guid), Category (another table, a many-to-many) Example of code added to the table Question 500 000 rows.
public void AddFive()
{
var q = new Question();
int counQ = 0;
for (int i=1; i<=100000; i++)
{
foreach (var x in GetUsers()) // 5 пользователей
{
++counQ;
q = new Question
{
NameQuestion = "TestQuestion" + counQ,
DetailsQuestion = "TestQuestion" + counQ,
UserId =x,
Category = GetcategoryList()
};
_context.Question.Add(q);
}
_context.SaveChanges();
}
}
public List<Guid> GetUsers()
{
List<Guid> userList = (from x in _context.aspnet_Users
select x.UserId).ToList();
return userList;
}
public List<Category> GetcategoryList()
{
var category = new List<Category>();
Random rand = new Random();
var count = rand.Next(1, 7);
for (int i = 1; i <= count; i++)
{
var idCategory = rand.Next(1, 7);
category.Add(_context.Category.Find(idCategory));
}
return category.ToList();
}
How can I speed up the process?