Search code examples
c#entity-frameworkentity-framework-6addrange

EF 6.0 DBSet.AddRange() - How to skip individual entity errors


I am trying to push 100 Product entities created dynamically, using DBSet.AddRange().

If for example, 1st Product entity has some data issues and throws exception during Commit(), all remaining 99 product entities also not going to save.

Is it possible to skip corrupted entities only so that other entities can be saved using DBSet.AddRange() ?


Solution

  • You cannot do this with EF, you have to change your logic and validate the entities yourself before adding them to the EF context. Alternatively, you have to add them individually and commit after each one, but that will be much less efficient.