Handling Errors with EF

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Handling Errors with EF

Nikolaus Kern

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 in this.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?

 

Thanks

 

Niko


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling Errors with EF

Jiří Činčura-2
What do you mean by "gentle way". You want to skip that record? Or skip
all pending changes?

Also do you have proxy creation enabled?

--
Mgr. Jiří Činčura
https://www.tabsoverspaces.com/


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling Errors with EF

Nikolaus Kern
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);

// ctx.SaveChanges() fails

foreach (ARBEITSZEITEN a in Arbeitszeiten)
{
        ctx.Entry(a).State = EntityState.Detached;
}

ctx.Entry(res).State = EntityState.Detached;

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.

Niko

-----Ursprüngliche Nachricht-----
Von: Jiří Činčura [mailto:[hidden email]]
Gesendet: Montag, 31. Juli 2017 16:09
An: [hidden email]
Betreff: Re: [Firebird-net-provider] Handling Errors with EF

What do you mean by "gentle way". You want to skip that record? Or skip all pending changes?

Also do you have proxy creation enabled?

--
Mgr. Jiří Činčura
https://www.tabsoverspaces.com/


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling Errors with EF

Jiří Činčura-2
> 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.

--
Mgr. Jiří Činčura
https://www.tabsoverspaces.com/

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling Errors with EF

Nikolaus Kern
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.

foreach (BAU2ORG bau2org in this.NeueBaustelle.BAU2ORG)
{
         this._ctx.Entry(bau2org).State = EntityState.Detached;
}

Niko

-----Ursprüngliche Nachricht-----
Von: Jiří Činčura [mailto:[hidden email]]
Gesendet: Dienstag, 1. August 2017 11:15
An: [hidden email]
Betreff: Re: [Firebird-net-provider] 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.

--
Mgr. Jiří Činčura
https://www.tabsoverspaces.com/

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling Errors with EF

Jiří Činčura-2
> 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).

--
Mgr. Jiří Činčura
https://www.tabsoverspaces.com/

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling Errors with EF

Nikolaus Kern
Hello,

this is the current config:

ctx.Configuration.ProxyCreationEnabled = false;

// res is the main object = an employee
// res.ARBEITSZEITEN are the list of past and current engagements

res = ctx.RESSOURCEN.Include(z => z.ARBEITSZEITEN).FirstOrDefault(a => a.EXTERNE_ID == personalnummer);
// In the case of an error:
foreach (ARBEITSZEITEN a in res.ARBEITSZEITEN)
{
        ctx.Entry(a).State = EntityState.Detached;
}
ctx.Entry(res).State = EntityState.Detached;

Stacktrace of the inner exception:

   bei System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   bei System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
   bei System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
   bei System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
   bei System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
   bei System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   bei System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   bei System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
   bei System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   bei System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   bei System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
   bei System.Data.Entity.Internal.InternalContext.SaveChanges()


Thanks

Niko

-----Ursprüngliche Nachricht-----
Von: Jiří Činčura [mailto:[hidden email]]
Gesendet: Dienstag, 1. August 2017 11:52
An: [hidden email]
Betreff: Re: [Firebird-net-provider] 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).

--
Mgr. Jiří Činčura
https://www.tabsoverspaces.com/

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling Errors with EF

Jiří Činčura-2
> Stacktrace of the inner exception:
>
>    bei
>    System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
>    bei
>    System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator
>    ut)
>    bei
>    System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T
>    noChangesResult, Func`2 updateFunction)
>    bei
>    System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
>    bei
>    System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
>    bei
>    System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1
>    func, IDbExecutionStrategy executionStrategy, Boolean
>    startLocalTransaction, Boolean releaseConnectionOnSuccess)
>    bei
>    System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions
>    options, IDbExecutionStrategy executionStrategy, Boolean
>    startLocalTransaction)
>    bei
>    System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
>    bei
>    System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1
>    operation)
>    bei
>    System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions
>    options, Boolean executeInExistingTransaction)
>    bei
>    System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions
>    options)
>    bei System.Data.Entity.Internal.InternalContext.SaveChanges()

This is from SaveChanges. It's not related to setting state to Detached.

--
Mgr. Jiří Činčura
https://www.tabsoverspaces.com/

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Firebird-net-provider mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider
Loading...