Sometimes I am importing 5000+ rows (of different entity types) into my DB. I use Entity Framework and I want the context to save the rows that fits on my model, if there are invalid ones they should be discarded.
I am inside a transaction, so : I need to decide at the end of the iteration if i want to Complete() the transacion or Dispose() ,It depends on which kind of entity throws the exception.
I think there are 2 possibilities:
- Check if a entity object is valid before context.Add(object)
- Add the 5000 items and, set in some way that context.SaveChanges() , saves the valid one and discard the invalid ones (now it throws an exception and discard all added items).
I am asking for a snippet of code, or some indications i can follow to solve that situation. I want only to import the valid items and generate a log of the invalid ones.
Thanks. I will mark as an answer if you solve it.