I am using Entitiy Framework and have come across a weird problem.
I am trying to save a collection to the database (say: Collection of Rounds).
Now each item in this collection in turn has a collection of child elements (say: Collection of Events).
Which would look something like this:
Round 1
(No Child Elements)
Round 2
Event 1
Event 2
Event 3
Round 3
(No Child Elements)
Round 4
Event 1
Event 2
As shown above it is not necessary that the parent object will always have a child collection.
Now here is the problem:
My requirement is that i want to save the data as i have added it to the collection.
But, while saving EF saves the items having a child collection first and so the order is modified upon saving.
So, in the database Round 2 is saved first and then others are saved randomly.
Is there any way to force EF to save the Rounds collection in the order that i have constructed it??
It should always save starting from Round 1 and should end with saving Round 4.
Thanks guys : )
Neither database or EF guarantees ordering. In the same way if you query the database you don't have to get elements in expected order. If you want exact order you must add additional column to keep record's ordering value and use OrderBy extension method when retrieving data.
Order of operations executed by SaveChanges is in full control of EF. You cannot change it.
Related
Assuming I have a table sets with a field filters, containing array of key - value mappings, table items to which select query must be applied to extract rows based on these filters, and associated table for M:M relations to link each set with each item. I am seeking for a method or mechanism to cancel select query if sets.filters were updated, otherwise M:M relation will be built invalid as based on yet not refreshed filters.
The concrete scenario when a problem takes place is:
Receive file with items data, to parse, and insert into items returning new relevant ids(primary keys here);
After insertion, select from relevant sets for filters;
Take items ids and select from items using filters;
Update M:M association table for all the items returned at step 3.
So, unfortunately between step 3 and 4 or even earlier, API call makes an update on one of the sets rows, changing its filters. As the result - M:M table is invalid, because one filter was changed(lets say the filters contained kind of weight <= 100 kilos expression, however after the mentioned update it has become weight <= 50 kilos, so if there are some new items with weight greater than 50, those items ids should not be in M:M table, obviously).
Is there some efficient way to cancel select query from items during transaction? Or maybe there is a strong query to use. My idea is to rollback changes post-factum, checking sets.modified_at column. But it seems as doing additional job by wasting disk and cpu time.
Is there any simple way to get the object, used to render given row in CellTable, by index of the row?
I am using AsyncDataProvider, and don't want to remember lists of data objects, returned from the server. Also I am using MultiSelectionModel, so several items could be selected there and I need to track out which one was clicked last.
I know the index of last clicked row, so I need to get the object, corresponding to the row, somehow.
getVisibleItem? possibly combined with getPageStart if you're using paging and you only know the absolute index.
For your use-case, maybe you could use a customized selection model whose setSelected tracks the last change.
We have a need to get the entities being added, modified or deleted in the same order as which it is being done on the database.
We are using the below code snippet by overriding the "SaveChanges" method in ObjectContext class to fetch the different entities who have been either added, modified or deleted.
foreach (ObjectStateEntry entry in
ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted))
{
// Operations on each entity
}
Assume that we have two tables A & B in the database. These are unrelated. We do the below operations on these table using entity framework.
Insert a new row in A
Update a row in B
Insert a new row in B
Update a row in A (not the one inserted in step 1)
Delete a row in B (Not the rows affected in step 2 & 3)
Insert a new row in B
Insert a new row in A
While using the above code snippet, we noticed that the inserts, updates and deletes were grouped together, i.e., we got the entites in order 1,3,6,7,2,4,5.
It is mentioned in this post (http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d8448144-05dd-4c34-b93c-9336c3b62f6e/) that there is no definite way to determine the order of inserts in operations done before a "SaveChanges".
Does the above code snippet fetch the correct order in which these operations are executed on the database.
In case this order is incorrect, is there any other way to get the correct order.
No you cannot change the order of operations. The only way to control the order is to run each operation separately (call SaveChanges after each operation).
Lets say I'm trying to RECORD as well as calculate results based on values stored in core data.
Lets take Enery equation : E=mc(square)
I have three entities, Parent/Child/RecentRecords
A Category's entity
Category
-Name : Einstein's equation
-E = mc2
Child Entity:
- Name: Mass
another entry:
- Name:LightSpeed (for the sake of example, lets say the speed of light is not constant.
Recent Entity: The purpose of this entity is to store and track the Child Entities and so it has:
-NSDate TimeStamp
-NSNumber: Value
-Relationship <---->>ChildEntity
I'll explain from a User's point of view,
If I tap "Einstain's equation" (Category/Parent Entity), the detail view will bring out 2 textfields by iterating through the Child entity. When I record my entries of mass, light speed, it should be saved in the Recent entity. SO that overtime i can have a bunch of recordings.
Now image a similar setup for another formula, v=U+at. where Velocity would be parent. U, a, t would be in CHild entity. And in one recording a user would add three Recent Entity objects.
My question is I want to find out if theres an efficient way to calculate the results/entries at the Parent/Category entity level.
Because, EinstainsEquation stores the formula of its child entities, E=mc2 and V=u+at. The thing thats making this complicated is fetching the entries from the recent Entity based on timestamp (recording) and calculating it and showing the result based on the most recent entries.
Parent<-------->>Child<-------->>Recent
-Einstein <---->>Mass <----------->>Multiple Mass recordings based on time
-mc2 LightSpeed <------>>Multiple Lighspeed records based on time
-Velocity<------>>U <--------->>Multiple entries based on time of all 3.
-u+at Aceleration
TimeTaken
Based on the graph above, I need to get einstein result FROM the most recent entries of mass/lightspeed and calculate each of those entries with its formula mc2.
I thought of having a transient Property in Parent entity, how would i go about accomplishing this? How can i use the formula and calculate the child values of recent entity.
I've read multiple tutorials but most of them have to do with basic setup.. I hope ill learn more each day
1) Does clicking index property of an entity automatically creates index of that property as the data is entered and its not required to enter the index ??
2) If an entry is removed from the middle, for example with index no 3 .. then will the new indices will be 0 1 2 4 5 6 .. or will it update itself to 0 1 2 3 4 5 ... ??
3) How to remove single entry for say.. row ID 3 ??
4) How to edit an existing entry at any ID ??
thnx in advance ; )
1) The indexing is handled automatically by Core Data. You don't do anything with it.
2) Don't know, don't care because Core Data handles it.
3) You don't. Core Data is not a wrapper for SQl and does not concern itself with tables and rows. Core Data uses objects. When you delete a object Core Data removes it from the store.
4) You edit data by editing the properties of a specific managed object.
Core Data is not SQL. Entities are not tables. Objects are not rows. Columns are not attributes. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. Trying to think of Core Data in SQL terms will cause you to completely misunderstand Core Data and result in much grief and wasted time.
making a property indexable is a performance option. It does not add a sortable index that you can access from your application. What it does is make that property faster to search if you are using a SQLite store.