Print Page | Close Window

Saving Order

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3261
Printed Date: 04-May-2024 at 7:38pm


Topic: Saving Order
Posted By: Vonzkie
Subject: Saving Order
Date Posted: 13-Feb-2012 at 1:38am
Hi,

How do I order entities to w/c should be saved first to the database?
Example I have a table named tblOrders that has a primary key of Order ID.

When I add 3 orders it will looks like this:

 Order ID   Column A   Column B
- 100           Hello     Hello Again
- 101           Hi     Hi Again
- 102         H     H Again

When I call the save method of the entity manager, what will happen is that the orders will be saved in reverse order.
Why is that so? and how do we save it in the order we want? In this case, we want it to be saved in the order we see it right now..

Thanks,
Von 





Replies:
Posted By: sbelini
Date Posted: 13-Feb-2012 at 8:43am
Hi Von,
 
Unfortunatelly there is no way to control the order of the entities being saved within one SaveChanges/SaveChangesAsync call.
 
What I'd suggest is to call SaveChanges for each of the entities in the desired order.
 
Regards,
   Silvio.


Posted By: Vonzkie
Date Posted: 13-Feb-2012 at 4:59pm
Hi,

Hmmm.. I think that's not convenient in our part because if i have 1000 items, I'll save them one by one?
What we want to accomplish is simple, to save it in order we create it and not in reverse order.
Can you give us a workaround for this?

Thanks,
Von


Posted By: sbelini
Date Posted: 14-Feb-2012 at 11:56am
Von,
 
Unfortunatelly, this is how EF handles the saves and there isn't really any other way to handle this issue.
 
You could create some sort of automated partial SaveChanges:
 
foreach (var entity in listOfEntitiesToBeSaved) {
   mgr.SaveChanges(entity):
}
 
bear in mind that listOfEntitiesToBeSaved must be properly ordered and you will need to properly handle any other entities related to the ones being saved that might not be in that list.
 
Regards,
   Silvio.


Posted By: Vonzkie
Date Posted: 21-Feb-2012 at 11:31pm
Hi Silvio,

I'm trying to do your work around but it seems that there's no overload for SaveChanges() that accepts a single entity but rather an IEnumerable of entities.

Thanks,
Von


Posted By: sbelini
Date Posted: 22-Feb-2012 at 9:55am
Hi Von,
 
try an IEnumerable of entities:
 
mgr.SaveChanges(new Entity[] {entity});
 
Silvio.



Print Page | Close Window