Entity Framework Bulk Update

We often need to update items in bulk while working with Entity Framework. As good as it is, entity framework has its quirks like any other ORM tools. Entity Framework bulk update is needed only when you need to update multiple items at a go.

We used classic ADO.NET stored procedure for our long-running processes. But when updating multiple items from models at once we found it rather a tedious process to maintain. We were already using the entity framework in our project for data access along with the classic ADO.NET. So decided to use entity framework for bulk update operations.

In this very short article, I am showing a way how we have updated our list of objects using entity framework DB first. Here we want to update some particular property of a list into our database.

ItemObject = Object model from database
list = the list of items we want to update
ContextFactory.GetContext() = A factory method to get a particular context. This is our application specific method.

Here we updated just a few properties in our model. If we want entity framework to update the whole model we need to set the entity state property to modified for the whole entity.

After the loop is finished, save the context. The data will be updated in the database.

Entity framework bulk update or insert operation is slow. I’d recommend any other option if we can find to do such bulk updates. But in terms of code maintenance, we found it feasible in our case. Be sure to use it when you need this kind of operation.

I hope this helps you guys. If you have any suggestion regarding anything or how should I improve please don’t forget to knock. I’d really appreciate your feedback.

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type

This error occurred in our application with a particular case when we stored two procedures with the same name in two different databases. These databases were internally related. We used Entity Framework database first approach for our application.

As EF uses only class names to identify the type mapped in EDMX (namespaces are ignored) – it is a convention to allow mapping classes from different namespaces to a single model, the EDMX could not differentiate the two classes generated from the stored procedures. So it threw this particular error-

[code language=”xml”]
The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type

So the solution is:

  • Don’t use classes with the same unqualified name.
  • Name your stored procedures differently even if they’re in separate databases if you’re planning to use them in a single BLL.