Entity Framework Z Plus Batch Update
Entity Framework Z Plus Batch Update
I am using Entity Framework Z Plus Batch Update method. I am unable to proceed due to below issue. Actually Update method works good when i give static values like tagName="amir1"
. But I need to get the Tagdescription
from a web service or from another collection based on the tagId
, Update method is not accepting a extension method or any other method to accomplish my requirement.Its saying
tagName="amir1"
Tagdescription
tagId
"LINQ to Entities does not recognize the method 'System.String GetTagDescription(Int32)' method, and this method cannot be translated into a store expression.".
Hope my requirement is clear now. Please guide me if there is any other approach for my requirement.
Here is my code:
using (var context = new TrialsDBEntities())
context.tblTags.Where(x => (tagIdCollection.Contains(x.tagId))).
Update(m => new tblTag tagDescription = m.tagId.GetTagDescription(), tagName = "amir1" );
public static string GetTagDescription(this int i)
return "test" + i;
///Replace above line with call to database or web service call
getting some text by giving i as input
2 Answers
2
Write your code as follows:
using (var context = new TrialsDBEntities())
var tagsToBeUpdated = context.tblTags.Where(x => (tagIdCollection.Contains(x.tagId))).AsNoTracking().ToList();
//Only use this code block if your tagsToBeUpdated list is too large
Parallel.ForEach(tagsToBeUpdated, tagToBeUpdated =>
var tagDescription = GetTagDescription(tagToBeUpdated.tagId);
tagToBeUpdated.tagDescription = tagDescription;
context.Entry(tagToBeUpdated).State = EntityState.Modified;
);
//Only use this code block if your tagsToBeUpdated list is not too large
foreach(var tagToBeUpdated in tagsToBeUpdated)
var tagDescription = GetTagDescription(tagToBeUpdated.tagId);
tagToBeUpdated.tagDescription = tagDescription;
context.Entry(tagToBeUpdated).State = EntityState.Modified;
context.SaveChanges();
public static string GetTagDescription(int i)
return "test" + i;
///Replace above line with call to database or web service call
//getting some text by giving i as input
Look at your error message! You cannot do that. From performance standpoint what's wrong? How many tag will be updated at a time?
– TanvirArjel
Sep 18 '18 at 4:40
I am going to save 50,000 records. I planned to call context.saveChanges once for 1000 records. So 50 batches.Hope that should be good.
– jamir
Sep 18 '18 at 4:56
Then use Parallel.Foreach(). I am updating my code.
– TanvirArjel
Sep 18 '18 at 4:57
@jamir I have updated the code!
– TanvirArjel
Sep 18 '18 at 5:07
Disclaimer: I'm the owner of the project Entity Framework Plus
Unfortunately, that's not possible to use BatchUpdate with a value that changes from a row to another.
Disclaimer: I'm the owner of the project Entity Framework Extensions
In this situation, we normally recommend using our paid library that's build for this kind of situation and offer high-performance saving operation.
Example
// Easiest way
context.BulkSaveChanges();
// Fastest way
context.BulkUpdate(tags);
Thanks Jonathan. I found it already that your Bulk Update would help. Thanks for sharing your thoughts. But its paid library. :(
– jamir
Sep 18 '18 at 12:27
Paid library must exist to allow to support free one ;)
– Jonathan Magnan
Sep 18 '18 at 14:27
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Thanks Tanvir. This is Traditional Way. Will it have good performance? For performance only i am going to Z plus Entity framework.
– jamir
Sep 18 '18 at 4:37