I have a web services that returns some data, which is then transformed into new entities on the server and then passed to the client, where user can edit them. If he chooses to SaveChanges
, the entities should be submitted to the server and inserted into database. I have two problems:
- When entities are returned to client, Breeze marks them as
EntityState.New
- Breeze expects, that unchanged entities have primary key set. Because none of the returned new entities have the key set (the key value of type Int32 equals to 0), Breeze thinks, that the server returned multiple instances of the same entity
To demonstrate the problem, change the ToDosController.ToDos
method in AngularJS sample to match the following:
[HttpGet]
public IEnumerable<TodoItem> Todos()
{
return new TodoItem[]
{
// Keys are not set because (equals to 0) these are new entities
new TodoItem() { Description="First item"},
new TodoItem() { Description="Second item"},
};
}
When you run the sample, the HTML page will shows two lines, both will have description "Second item". If I explicitly set the Ids of those items on the server (which I do not want to do, because the keys are generated by database), the problem is not manifested.
The question: how to correctly return entities from the server, so that they will be marked as EntityState.New
and they will be saved into database (with generated keys) when SaveChanges
is called.
I would expect some MergeStrategy
on the client or some extra data/attribute on the server to achieve this, but was unable to find one.
UPDATE:
To clarify:
I am try to support a scenario when users selects and edits one of the entities which originate from some other source, that should be later added to my database.
In details:
Client calls the server with search criteria as method parameters. Server method returns
IEnumerable<Customer>
- it does not returnIQueriable<Customer>
Server queries a backend web service (CRM system) and transforms the result of backend web service into
Customer
entities. The result is returned to the client. The backend web service is not available to the client.Client displays the result to the user
User selects one of the entities and edit its properties (such as change customer name or address)
The selected customer is added to entity manager on the client. It should be in the
EntityState.Added
em.SaveChanges
is called, which submits theAdd
ed entity to the serverServer inserts the new customer into database, database generates new primary key, which is returned to the client, where EM updates the entity key and set entity state to
EntityState.Unchanged
Maybe I need detached entities and the right question is: "How to return entities to the client without adding them to entity manager?" (they will be added in step 5 above).
P.S: One solution would be to use a custom, non-entity datatype (such as CustomerFromCRM) as result of my server method. I would then transform them to entity Customer on the client side. But I would like to avoid creating additional classes.
UPDATE2: I have found a similar question (with no accepted answere) here: is there an easy way to mark an entity in the cache as "added"?