I am working with Silverlight application with MVVM Concept and Entity framework and having some trouble in inserting the values. Using SubmitChanges method to update, insert, or delete data.
All of the pending changes are submitted in one operation.
Is it possible to use SubmitChanges method twice in one process?
Because I have below requirement:
In First step am inserting data into Table B.
Second step am inserting data into Table A. Here Table A has FK relation to Table B. That's why I am inserting data(Table B) in first step only.
But problem is: submitChange method insert all pending changes at once.
I need to submit the data twice in one process.
Entity Framework will handle the inserts correctly only if you setup the relationships correctly. You do not have to worry about the sequence of the inserts. Entity Framework will sort that out automatically. As a test, open a new diagram of your database and you should see a one to one or one to many relationship between TableA and TableB. If you do not see this relationship, EF will not know how to do the inserts.
Entity Framework will infer the sequence. If you check your object just before the context inserts the record, you should see zero's in the identity columns of both Parent and Child objects. EF will hydrate these properties with the identity values once the record(s) have been inserted.
Related
I have EntityA which has a navigation property to EntityB. In the frontend it's possible to create a new EntityA and append it to EntityB. If I now try to save the new created EntityA, Breeze also want's to save the changes on EntityB (containing the new ID of the newly created EntityA). Is it somehow possible to avoid having EntityB, because in this specific use case it should be possible to append new entities to EntityB, but these should not be saved back (and also not be reported as pending changes)?
I see the possibility with using two EntityManagers, but this would mean that I can no longer have navigation properties between the two types.
Pascal is asking an important question: are Entity A and Entity B related one-to-one? More to the point are they related one-to-one such that A depends on B (i.e. A is a child of B)?
A typical relationship of this sort is the "extension" entity. Consider "Order" and "OrderExtension". "OrderExtension" is a bolt-on type with optional fields that "extend" the core order data. An order can have zero or one "OrderExtension" records.
Order is the parent in this example; it SHOULD NOT have a FK reference to the OrderExtension. The OrderExtension is the child and it SHOULD HAVE a required OrderID FK field. The parent Order can exist without a child, but the child OrderExtension cannot exist w/o the parent.
At least that's how I think it should be. I've often seen folks turn this around. They give the Order an OrderExtensionID FK field which is optional. The OrderExtension has no backpointer to the Order.
The weakness of this design is that it allows you to create multiple orphaned OrderExtension entities that don't belong to anything ... and you'll rarely know they are there.
I'm betting that's your situation. I'm betting that Entity B is like OrderExtension and Entity A is like Order. When you created the OrderExtension (B) and associated it with an Order (A), Breeze tried to maintain that relationship for you by updating the Order.OrderExtensionID property. That puts Order (A) in a modified state.
DO not proceed until you've figured this out. While Jeremy is correct that you can save one entity by cherry picking the pending changes - you can save B without saving A -, you risk breaking the integrity of your data!
From a modeling perspective you've made Entity A dependent on Entity B. If you don't save A at the same time you save B, there will be no way for someone using the database to know that the two are related.
Next time you query for either of them, neither you nor Breeze will know they are related. You will be unable to navigate between A and B. I'm pretty sure that's not what you had in mind.
You can pass an array containing the entities you wish to save to the saveChanges method to restrict which entities are saved.
From the breeze docs:
saveChanges ( [entities] [saveOptions] [callback] [errorCallback] ) async
Saves either a list of specified entities or all changed entities
within this EntityManager. If there are no changes to any of the
entities specified then there will be no server side call made but a
valid 'empty' saveResult will still be returned.
Parameters:
[entities] Array of Entity optional The list of entities to save.
Every entity in that list will be sent to the server, whether changed
or unchanged, as long as it is attached to this EntityManager. If this
parameter is omitted, null or empty (the usual case), every entity
with pending changes in this EntityManager will be saved.
I have a problem in core data where I am using both one-to-one and one-to-many relationships.
1. Lets consider a parent entity P1 and child entities C1,C2,C3.
2. C3 has 3 more child entities C31,C32,C33.
3. If I update or delete or change in C32 table, I will get full JSON from the server for the parent P1.
Is there any way to changes all child tables of a particular parent table?
Basically, if any update or delete or change is done in child tables, I will get a new parent table with all child tables details from server.
Is there any way to handle this in core data?
More than handling relationship in Core Data, you question is about how keeping in sync a remote database exposed through a JSON-based API and your local Core Data mirror.
The naive way, in the scenario you depicted, is:
you get a full JSON from the server;
with this, identify your parent entity (based e.g. on its ID);
delete the parent entity from your local database; if you set up your model properly, all children will w also be deleted;
create the entity anew with all of its children.
The other possible approach, is you get the full JSON, then compare each child in the JSON with each child in Core Data and delete those not present in the JSON.
Finally, you could take into consideration RestKIT, which is a framework which will do all of this work for you -- there is some learning curve to go, but it will handle all details for you.
We would like to create a history table for each of our entities.
Is there a way to have entity framework create the history entities/tables automatically, given the existing entities?
For example, the Customer entity would have a shadow history table called CustomerHistory. Whenever there is an edit or a delete, the old record will be inserted into the history table. The main entity table will always contain the current record.
Since we have several entities, I would rather not have to create a separate history entity and repository for each entity, thus doubling the entities.
We would like to do this for all of our entities in order to track transactional and state temporal changes to the entities.
We use the unit of work and repository patterns.
Assume that the properties of the Entity and the EntityHistory objects would be the same, although we may want to extend the history object with some datetime properties.
I understand how to override DbContext.SaveChanges in order to write to history/audit tables.
What I want to avoid doing is having to create duplicate history entities/repositories for every entity that we have.
I am currently exploring doing something in OnModelCreating to create a history entity for each entity but have not found a good example.
Any ideas?
In Entity framework 4.0 how can we fetch multiple record set from database in one call like we do in ado.net dataset?
Soppose we have 3 table T1,T2 and T3. We need to fetch data from all tree table and pass to view(ASP.NET MVC3). No JOIN is to be used as all are independent table. Instead of making 3 call to database we want to wrap up all select statement in one SP and make only one call to database and pass all data to view.
In case of dataset if stored procedure return data from multiple select statement dataset populate each recordset in different table.
How can we achieve it in EF? Please help me.
Thanks,
Paul
There is no out of the box feature to batch queries in EF. But there are some efforts made by others to extend EF to support this.
Entity Framework Batch Update and Future Queries
MultiQuery
(more queries in one batch) in Entity Framework using LINQ
I have a similar challenge to this post: Batch insert/update with entity framework from a couple years ago, I was hoping that the story may have changed since then.
In short, I am running a RESTful service, and as such I'd like a PUT to be document-oriented and take an object along with a collection of child elements in. The child elements have a unique string that I can use for determining existence.
Unlike the referenced poster, I don't have a query requirement; all I want to do is be able to take in a collection of my child elements and perform an insert on the child table for any that aren't already there, and an insert or delete on the many-to-many table to account for the current state of the collection. Ideally, with some efficiency. I realize that I might end up doing this as a sproc, I just wanted to see if there's an EF-native way that works first.
To do this you must either know which items are new or you must query DB first and merge your received items with loaded items. EF will not handle this for you. Also be aware that there are still no batch modifications. Each insert, update or delete is executed in separate roundtrip to database.