Batch insert / update with entity

  • I have a tag table whose schema consists only of an identifier and a name (unique). Now from the GUI the user can enter tags for BlogPost. When data is saved, with tags stored in an array of strings (names), I want to add tags whose names do not yet exist in the tag table, and ignore tags whose names already exist, and return a list of all tag objects (including existing and newly added ) How can I do this in the Entity Framework in just one SQL tour?

  • For the returned tags, I want to associate them with the added BlogPost object (which has just been created and has not been saved to DB via EF yet). Is it possible that this step can be combined with C # 1 in one single circuit or will I need to issue another request?

+4
source share
1 answer

I do not think Entity Framework does not insert inserts at all (at present). Therefore, if you must keep the number of hits in the DB so low, you probably have to use a stored procedure or a database trigger. Fortunately, the Entity Framework supports stored procedures that return object types. There is MSDN documentation about this. You can create a proc that takes a string list of tags and returns instances of the instance entity. Alternatively, you can add a VARCHAR column to your message table for the tag splitter list and analyze it in a trigger.

+6
source

All Articles