Entity Framework 4.1 DatabaseGeneratedOption.Identity Performance

本秂侑毒 提交于 2020-01-11 08:01:30

问题


I currently working on a application that use Entity Framework 4.1 (code first) and SQL Server CE 4.0. One of the new things I really like from SQL Server CE 4.0 are the computed values.

But I have some performance problem while importing old data into my new data store system (around 50000 entries). This is my second implementation for this. The first version used Entity Framework 4.0 and SQL Server CE 3.5. But there is a huge performance difference between these implementations. The code first implementation takes around one hour to import the entries, the other implementation only some minutes.

The most time is spend in the SaveChanges method. I tracked the problem down to the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute. If I use [DatabaseGenerated(DatabaseGeneratedOption.None)] and implement my own generation of keys (as a simple workaround) the performance is back on the level of the first implementation.

Is this a known problem? Is there any way to solve this performance problem? Or is generating my own keys the way to go?


回答1:


Keep an ObjectContext / connection open for the liftime of your app. EF is not ideal for bulk inserts, use SqlCeResultSet for that for max perf.




回答2:


I decided to go the "generate my own key" way as a workaround. I added the required features for key generation to my DBContext class.

If Microsoft change something on this behavior, I switch back to auto generation.



来源:https://stackoverflow.com/questions/6642274/entity-framework-4-1-databasegeneratedoption-identity-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!