Quantcast

Detaching navigation properties

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

Detaching navigation properties

Nikolaus Kern

Hello,

 

I am currently using EF 6.x with Firebird 2.5.x and it works very well.

 

During a dataload from an external datasource I load a number of records from my database and compare them with the external source. Sometimes I have to correct/complete my data. In the case this update fails on the database i detach the current entity that the following updates can continue.

 

This works fine unless I have modified a navigation property of the current entity.

 

List<RESSOURCEN>  = this.RESSOURCEN.Where(a => a.RESSOURCEN_STATUS == "Aktiv").Include(b => b.ARBEITSZEITEN).ToList();

// I loop through the List<RESSOURCEN>

// I modify one of the navigation properties ARBEITSZEITEN

// The update fails

this._efContext.Entry(res).State = EntityState.Detached;

// Now the entity RESSOURCEN res is detached and all navigation properties are set to null. I understand the reason for that, following this page: http://stackoverflow.com/questions/10342445/why-does-setting-entitystate-to-detached-empty-a-property-of-type-listt#10343174

 

// I tried to detach the navigation property before the entity itself, but this fails: Change of items in the enumeration I am currently looping through.       

Foreach(ARBEITSZEITEN azneu in res.ARBEITSZEITEN)

{

this._efContext.Entry(azneu) State = EntityState.Detached;

}

 

// I added now the modified navigation properties to my own list, which I use then the detach.

modifiedAZ.Add(az);

// In the case of a error during update on the database

Foreach(ARBEITSZEITEN azneu in modifiedAZ)

{

this._efContext.Entry(azneu) State = EntityState.Detached;

}

modifiedAZ.Clear();

 

 

How would you handle such a situation?

 

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: Detaching navigation properties

Jiří Činčura-2
Do you really have to detach? Wouldn't work for you just fine to set the
state to unchanged (nor sure what's done with the entities after)?

Other than that, I believe putting it into list/array is OK (at least
for me). You might even just use ToList method and foreach on that,
without the extra variable - but the intermediate list will be created
anyway, so it's just a cosmetic thing.

--
Mgr. Jiří Činčura
https://blog.cincura.net/


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