c# - Multithreading within entity framework context -


I have a very large query, which I want to execute in multiple threads to maximize performance. Here is a generalization of my code:

  Public Operations Control Act () ({var reference = New data entry ()) {var person = context.Poople.First (p = & gt; p UserId == User.Identity.UserId); var model = new person view model (person); See Return (Model); }} // Inside the Public List of the Person View Model class & lt; ApplicationView Model & gt; {Get; app; Set; } Public listing & lt; Message View Modes & gt; Message {receive; Set; } Public person view model (person person) {var applications thread = new thread (populate applications); applicationThread.Start (person); var messithread = new thread (populated message); messagesThread.Start (person); // Other code to be done when application / message threads run the application. Thread.Join (); messagesThread.Join (); } Private zero populateApplications (Object person object) {var person = personObject as person; Application = person. Application. Where (/ * argument * /); } Private zero populatemessages (Object object object) {var person = personObject as a person; Message = person message. Where (/ * argument * /); }   

However, when I hit that controller action, only the application has populated, I'm guessing that the unit with several threads trying to use the properties of the reference object There is something, but none of these should not collide with each other. This concept has been strengthened by the fact that due to the switch of the thread order the messages can be populated, but the application Nothing will remain unchanged.

Edit, here is the updated version of Krillger's answer:

  private static async zero generateAbilities (int personId) {(var reference = new BattleGameEntities ()) {var personAbilities = Waiting Context.spersons. First (P = & gt; p.personId == person ID). Choose the message. OrderB (M = & gt; m.Date). (A = & gt; New MessageView Model {// argument}}. ToListAsync ();}   

}

Correct is the fact that DbContext is running on DataReader. Only of them can be opened at the same time.

If you Using NET 4.5 and EF 6, you can use the new Async () methods to automatically do many threads for that, in reality it is simply a suffix on any method There is an article on using these new methods.

  var applications = context.toListAsync (); var message = context.must.toListAsync ();   

If you use .NET 4.0 If you are doing, you will have to create a new reference in each thread, and keep them separate. At that point, you will also go into issues where the objects are coming from different contexts.

Yes, I know that the article is in VB.NET, but the syntax is the same. For whatever reason, this is all their articles Has been written in.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -