Reputation: 3934
I have object "Stage Details", when I create new Entity this object, Then breeze automatically create primary key like as -1,-2,-3,-4 etc. And when this object is saved then I saw that the first object saved is last, That is the record -4 which was last record into my object list. I need sequentially save in breezejs.
I want to manage order on the server side. i am using EFContextProvider, i want to mange order before save changes. my code is
public SaveResult Save(JObject saveBundle)
{
//i want to manage order in this position
return _contextProvider.SaveChanges(saveBundle);
}
Is it possible? Thanks.
Upvotes: 1
Views: 537
Reputation: 17863
I start my answer by calling into question your premise that the id sequence matters.
Since you are using the EFContextProvider
you know that the save is transactional and either all of the batched items will be saved together or none of them will be. You're not losing data integrity if the server happens to save the items in reverse order of their creation on the client.
Relational databases have no notion of save order and it is fundamentally unwise to care about the meaning or sequence of keys. These are store generated keys anyway so why do you care?
Moreover, Entity Framework is free to save items of the same type in any order. You can't know if another user will complete a save either before or after you. If you try separate saveChanges
calls, it is likely that another user will insert items in the middle of your "batch".
If you're dead set on trying to manage EF's ID generation you can try to rearrange the entities in the SaveMap
object inside a BeforeSaveEntities
method and that might work most of the time.
I would not make multiple saveChanges
calls as that is bound to harm client and server performance, defeat transactional integrity, and cause other headaches without any certainty that you'll satisfy your intentions.
Upvotes: 0
Reputation: 54
not so sure that's what he needs. (this may result in mulitple calls the server which is unwanted most of the times) as far as i know breeze should know the order in which each entity should be saved. if it doesn't work like you expect then it might be a mapping issue\metadata issue but if you go with cherry pick (make your own save bundle in the order you want) then breeze will save the entities in that order. you can grab the entities from the manager by the entity types you need and filter out states you don't want like deleted or unchanged.
this should be true for NH i never tested this on EF but i think it should be the same
Hope this helps
Upvotes: 0
Reputation: 1117
In a scenario where you must create records in the database in a particular order that only the client knows about, you can call EntityManager.saveChanges
multiple times to save the entities in the order that your application requires. The following code illustrates an approach to force Breeze to save a set of entities in the backend database in the same order as they appear in an array of entities:
var entityManager = /* your Breeze entity manager */;
var arrayOfEntities = [/* array of entities that have been added to entityManager, in order they must be created */];
var saveEntities = function(i) {
if (arrayOfEntities.length > i) {
var e = arrayOfEntities[i];
entityManager.saveChanges([e]).then(function() { saveEntities(i + 1); });
// Error handling upon failure left as an exercise
}
};
saveEntities(0);
This will obviously cause multiple HTTP operations to be performed, but since I assume it is the client code which is where the knowledge about the ordering exists, I think that will be inevitable. A better approach may be to consider whether you really need to save/create entities in a particular order: it sounds a little unusual to have a situation where the order of creation of otherwise unrelated entities of the same type really matters.
Upvotes: 2