New Posts New Posts RSS Feed: Saving Order
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Saving Order

 Post Reply Post Reply
Author
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post Topic: Saving Order
    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 


Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Vonzkie View Drop Down
Senior Member
Senior Member
Avatar

Joined: 01-Aug-2011
Location: PH
Posts: 133
Post Options Post Options   Quote Vonzkie Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 22-Feb-2012 at 9:55am
Hi Von,
 
try an IEnumerable of entities:
 
mgr.SaveChanges(new Entity[] {entity});
 
Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down