Entity Framework 4.1 DatabaseGeneratedOption.Ident

2019-02-21 04:26发布

问题:

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.