Setting ID on child properties with EFBulkInsert - performance

So im using EFBulkInsert https://efbulkinsert.codeplex.com/
The problem is I have child objects I need to set the ID of the inserted parent objects.
Previously after inserting the parents objects I've tried to rely on the context to return the id's and set them on the child objects - then use EFBulkInsert to insert the child objects - every now and then the context gets confused even after recreating the context and I get the wrong id on the child objects.
Does anyone have a good pattern / strategy for setting the parent id on the child object I should mention i'm doing this for a batch of 1000 parent objects. So I don't particularly want to get the id's from the database after SaveChanges for the parent object unless it's performant.

The best idea I have is to add two temporary columns, firtst to hold an original Id, and second to hold parentId. After bulk insert update proper columns. This method requires privilages to modify the table.
I had a similar problem when I couldn't modify database, so I set AutoDetectChangesEnabled and ValidateOnSaveEnabled to false, but results were not very satisfied.

Related

Parse.com afterSave trigger for object with array of other objects

I need to analyse some data after it has been uploaded to Parse and saved but the data is an object that contains an array of other objects...
Parent
-------
children : [Child]
I'm just not certain if the afterSave will trigger before all the children objects are uploaded and saved? Is it guaranteed that they will be there on the afterSave of the Parent object?
Thanks
In general the answer should be yes, they must always exist.
If it's an array of plain (not parse) objects then the data is directly there. If it's an array of parse objects (so an array of pointers) then the objects have to exist before they have ids and pointers can be generated for them. Same goes for a relationship to other parse objects.
Now, once the relationship to a parse object has been created it is not required that the parent is saved after the child - that is up to your code to guarantee if you need to. Indeed, it could just be that the child is saved and the parent isn't dirty so doesn't need to be saved...

Magento: How to delete parent object when child reference changes?

Given:
Two custom classes in Magento with a Many-to-One relationship between them.
The child holds a foreign key to the parent.
The database is set to cascade deletes.
There are cases when a child's reference changes to a different parent. In some of those cases, I want to delete the parent in the afterSave method of the child. When I do this, the child itself disappears, since the change of FK to the new parent hasn't been written to the database yet, and the database level cascade kicks in.
How can I arrange for the deletion of the parent object after the write of the new foreign key in the child object?
afterSave triggers before the query has been written to DB, as you've noticed yourself. You need to use *_save_commit_after event. Where asterisk is your Models event_prefix. Create an Observer and listen for this event, that way you can be sure that info in DB has been already updated, and you won't suffer the foreign key effect.

how to use complex object to insert data in more than one table related "Entity Framework 4.1"

I would like to know is there any way to use complex type to insert data in more than one table
if I have a parent object has a collection of child object and I need to insert data in the parent and its child too
Example: This is the parent object "Video" and its Child VideoData Diagram
Please advice
EF will update all modified entities/properties automatically. So if you create a new VideoData, set it's language, and append it to the VideoDatas collection of a Video instance, then call
context.SaveChanges();
EF will add the VideoData to the db, setting the Language, then update the Video record. It walks up and down the graph saving any changes. You just need to make sure you are using context to get the entities in the first place so EF can track it.

Using LINQ to delete child records automatically

I am somewhat new to LINQ and have a quick question regarding deleting.
Say, for example I have 2 tables, Orders and OrderItems. Using LINQ, I can easily create a new child record by using
order.Items.Add(new OrderItem());
and this will create the child record in the database and update its foreign key to the orderId. This is great, I like it! However when I want to remove a child record
order.Items.Remove(orderItem);
I get an error when I sumbit the changes (because its not actually deleting the child row (order item), just removing the foreign keyId). Is it possible to do this the way I would like to? I don't want to have to create a whole bunch of repositories and if ladders to delete all child rows for a large database.
Thanks in advance.
E
You can achieve that in the DB itself by configuring the Foreign key relationship to delete child records on deletion of the parent's key.
Note that this is transparent to Linq2SQL and it will not be aware of it, so it's best to make sure you do not keep the datacontexts around after that, since the OrderItem objects will still be present.
Set ON DELETE CASCADE for the table in question which will let the SQL Server handle this for you.

issue with submitChanges() inserting unwanted records in linq

I am using LINQ to insert records in the database. I create these records and keep track of them using a List. Based on some logic, I delete some of the records by deleting from the List. (I am using the same DataContext object).
When I want to insert the records in the database, I do the corresponding linq table's InsertOnSubmit() followed by SubmitChanges() on datacontext object. LINQ inserts the deleted-from-List records too along with the ones that are present in the list.
example:
//list to keep track of records to insert
List list
// add the records to list
list.add(some records)
//deleted last 2 records
list.remove()
//call InsertAllOnSubmit on the linq table passing the list object with records to insert
linqTable.InsertAllOnSubmit(list)
//call SubmitChanges on datacontext object
datacontext.SubmitChanges()
I came across this msdn article Object States and Change-Tracking (LINQ to SQL)
You can explicitly request Inserts by
using InsertOnSubmit. Alternatively,
LINQ to SQL can infer Inserts by
finding objects connected to one of
the known objects that must be
updated. For example, if you add an
Untracked object to an
EntitySet(TEntity) or set an
EntityRef(TEntity) to an Untracked
object, you make the Untracked object
reachable by way of tracked objects in
the graph. While processing
SubmitChanges, LINQ to SQL traverses
the tracked objects and discovers any
reachable persistent objects that are
not tracked. Such objects are
candidates for insertion into the
database.
I guess the question boils down to this - how do I change the deleted objects' state to 'Untracked'?
I tried DeleteOnSubmit after I delete the objects from list but that gives an exception (Cannot remove an entity that has not been attached).
Can someone please point me to a solution? Thanks.
I would like to know if I can achieve this using LINQ only. (I know that I can use an stored proc and insert only the records in the list.)
I think that the elements of your list are not being removed, because the List.Remove method determines equality using the default equality comparer.
If you don't want to write a custom comparer, I recommend you to use the RemoveAll method, which receives a predicate as its first parameter to match the elements that will be removed from the list:
list.RemoveAll( e=> /*condition to remove the element*/ );
Or the RemoveAt method, that removes the element based on the specified index:
list.RemoveAt(0); // Delete first element

Resources