Handling Errors with EF

Handling Errors with EF

Hello,

I am using EF with Firebird for some time now and it works very fine.

When I submit a new record via EF i wrap it into a try{} catch{} and want to to recover in a gentle way. Eg. The new record violates the PK. In this case I set the current entity to state detached. This works ok unless the entity has navigation properties that have been populated.

this._ctx.Entry(this.NeueBaustelle).State = EntityState.Detached;

In this case I tried to detached them before the main entity:

foreach (BAU2ORG bau2org inthis.NeueBaustelle.BAU2ORG)

{

this._ctx.Entry(bau2org).State = EntityState.Detached;

}

Unfortunatly I receive a collection has changed error. Therefor I tried this:

this.NeueBaustelle.BAU2ORG.Clear();

So far I do not get any troubles (tests are in progress).

What is the best way to handle error conditions during save with navigation properties?

Re: Handling Errors with EF

Hello Jiri,

by gentle way I mean that one failing record does not impact the following context.SaveChanges().

Yes I want to skip the failing record. If the user complets e.g. missing data and submits the data a second time I will add the entity again and save it. During a mass import of data I also want to skip the failing record and continue.

Regarding proxy creation: It was enabled at the beginning, I disabled it, but did not see a difference.

This approach is currently working for me:

// Copy alle records of the relevant navigation property to a new list<T>.
Arbeitszeiten.AddRange(res.ARBEITSZEITEN);

Re: Handling Errors with EF

> I would like to understand if there is a better way to handle the
> relation between the main entitiy and navigation properties when a
> SaveChanges() fails.

It's generally expected that every change in DbContext (and hence in
UOW) will be saved. The user will eventually fix all validation
violations. Hence the skipping is kind of non-standard scenario.
Detaching the graph is then correct solution. Sadly you have to detach
the whole graph manually
(https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.detach(v=vs.110).aspx).
Given the DbContext is the UOW these no way to disconnect these - have
one DbContext with multiple UOWs (where you'd discard that UOW with
violations and continue saving). Or course poor man's solution would be
to create bunch of DbContexts, but I'm afraid that would hurt
performance and add complexity on the other side with attaching.

Re: Handling Errors with EF

Hello Jiri,

thanks for the fast response.

If I want to loop through the navigation property directy (without copying it to another List<T>) is there a way to do this? I don#t understand the collection changed error, because I do not add or remove an item from the list. I am changing the Entitystate.

> I would like to understand if there is a better way to handle the
> relation between the main entitiy and navigation properties when a
> SaveChanges() fails.

It's generally expected that every change in DbContext (and hence in
UOW) will be saved. The user will eventually fix all validation violations. Hence the skipping is kind of non-standard scenario.
Detaching the graph is then correct solution. Sadly you have to detach the whole graph manually (https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.detach(v=vs.110).aspx).
Given the DbContext is the UOW these no way to disconnect these - have one DbContext with multiple UOWs (where you'd discard that UOW with violations and continue saving). Or course poor man's solution would be to create bunch of DbContexts, but I'm afraid that would hurt performance and add complexity on the other side with attaching.

Re: Handling Errors with EF

> If I want to loop through the navigation property directy (without
> copying it to another List<T>) is there a way to do this? I don#t
> understand the collection changed error, because I do not add or remove
> an item from the list. I am changing the Entitystate.

What's the exception stack trace? That should work out of the box (with
the proxies turned off).

> If I want to loop through the navigation property directy (without
> copying it to another List<T>) is there a way to do this? I don#t
> understand the collection changed error, because I do not add or
> remove an item from the list. I am changing the Entitystate.

What's the exception stack trace? That should work out of the box (with the proxies turned off).