How to configure ProviderManifestToken for EF Code First

If you’re using EF 6 (just released) you have an alternative. Dependency Resolution You can use the new dependency resolution feature to register an implementation of IManifestTokenResolver (described in this preview documentation as IManifestTokenService). This article gives a bit more information on how to use DbConfiguration. The easiest way to use it is like this: … Read more

Batch update/delete EF5

There are two open source projects allowing this: EntityFramework.Extended and Entity Framework Extensions. You can also check discussion about bulk updates on EF’s codeplex site. Inserting 100k records through EF is in the first place wrong application architecture. You should choose different lightweight technology for data imports. Even EF’s internal operation with such big record … Read more

Entity Framework 5 – DbContext Has Changes?

For EF 5 use DbContext‘s ChangeTracker: public bool HasUnsavedChanges() { return this.ChangeTracker.Entries().Any(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted); } For EF 6 use the ChangeTracker.HasChanges() method which will also detect changes in many to many relationships: public bool HasUnsavedChanges() { return this.ChangeTracker.HasChanges(); }

Update wizard not responding

Setting the compatibility level of the database to 110 has worked for me. To check the compatibility level, run this script: select compatibility_level from sys.databases where name=”<YOUR_DB_NAME>” To set the compatibility level, use this script: alter database <YOUR_DB_NAME> set compatibility_level = 110

How can I delete 1,000 rows with EF6?

EF 6 as far as I know introduced the .RemoveRange() option on your DbContext. So in short, you can do something like the following: var db = new MyDbContext(); var itemsToDelete = db.MyTable.Where(x=>!x.active); db.MyTable.RemoveRange(itemsToDelete); db.SaveChanges(); So instead of having to do any type of foreach, you can utilize this new extension method. With your Unit … Read more

When does Entity Framework open and close Database Connections?

Basically it opens when a request is called and closes once the results have been disposed or consumed. You can manually open/close or use the same connection using the object context… This article has a more complete explanation – http://msdn.microsoft.com/en-us/library/bb738582.aspx (archive.org) Here is the How To on using an entity Connection – http://msdn.microsoft.com/en-us/library/bb738461.aspx (archive.org)

Entity framework performance issue, saveChanges is very slow

Turn off change tracking before you perform your inserts. This will improve your performance significantly (magnitudes of order). Putting SaveChanges() outside your loop will help as well, but turning off change tracking will help even more. using (var context = new CustomerContext()) { context.Configuration.AutoDetectChangesEnabled = false; // A loop to add all your new entities … Read more

Map two different entities to the same table?

You can’t map two regular entities into same table. You have several choices: Use table splitting. Use custom query with projection to non entity type (as @Aducci proposed) Use QueryView Use database view or directly DefiningQuery Table splitting Table splitting allows you to map a table into two entities in 1:1 relation. First entity will … Read more