开发者

Entity framework insert optimization

I need to insert a lot of data to table (table: 'File', server: mysql) using Entity Framework.

The 'File' table definition:

-Id, int, primary key, auto_increment

-Name, varchar(100)

Now I am using a code like this:

TestDatabaseEntities entities = new TestDatabaseEntities()

for (int i = 0; i < 100000; ++i)
{
    File f = new File();
    f.Name = "test";

    entities.File.AddObject(f);
}

entities.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

I don't need to get Id of inserted values. How I can improve insertion开发者_Go百科?


You can't improve it because EF doesn't offer any optimalizations or command batching. Entity framework is not a good tool for processing of big data batches. If you just need to initialize 100.000 records use stored procedure but If you need to insert 100.000 different records you will get much better results by using ADO.NET directly and executing for example 100+ inserts in single DbCommand. EF will always be like 100x slower because it will execute each insert in separate round trip to database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜