We are using EF Code First with SQL Server, and we have a rather large set of master data (~150.000 records) that has to be initialized when the database is created. It would be convenient if this could be done in the Seed
method of the db-intializer.
I have generated the SQL script initializing this data. The script consists of lines, where every line contains the INSERT operation for a thousand records. The reason for this is that INSERT commands have a limit of 1000 total records in a single command.
Thus the script is similar to the following:
INSERT INTO MyTable (A, B, C) VALUES (1, 2, 3), (2, 3, 4), ... 1000 records here ... (3, 4, 5);
INSERT INTO MyTable (A, B, C) VALUES (4, 5, 6), (5, 6, 7), ... 1000 records here ... (6, 7, 8);
.... 150 lines
The initialization part of our Seed
method looks like this:
foreach (string command in File.ReadAllLines("InsertMasterData.sql"))
{
context.Database.ExecuteSqlCommand(command);
}
However, this almost takes 2 minutes on my developer machine, using an SQL LocalDB instance (it will probably be faster on a full-blown SQL Server used in production, but it's convenient to use LocalDB for development).
Is there any way to make this faster?