开发者

Entity Framework 4.1 DatabaseGeneratedOption.Identity Performance

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 proble开发者_如何学JAVAm 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜