What is the best method to access a large database using MVC 4 and Entity Framework 6

user2789697

I have an email marketing web application written in MVC 4 and Entity Framework 6. The application stores more than 10K email addresses along with contact information in a sql database on a web farm.

Everything works fine while the database is small, a few hundred records. However, when I fully populate the database, it is painfully slow retrieving and writing each individual record using "Foreach" statements. For some operations it can take up to 14 mins to complete. I have tried to minimize the number of records I work with at any one time, but it make the application clumsy.

I was wondering, are there other methods I could use to speed this up. Perhaps SQL stored procedures, or something on GitHub that would make it easier. If there are, please let me know where I can learn more about it. Any help would be appreciated. Here is one of the queries.

private int AddOrUpdateCampaigns(Campaign campaign
        , IEnumerable<AssignEmailContacts> assignEmailContacts)
{
    DataBaseContext db = new DataBaseContext();
    int TotalAssigned = 0;
    if (assignEmailContacts == null) return(TotalAssigned);

    if (campaign.CampaignId != 0)
    {   
      foreach (var emailContact 
                in assignEmailContacts.Where(a => a.Assigned == false))
      {   
        campaign.EmailContacts.Remove(
                db.EmailContacts.Find(emailContact.EmailContactId));
      }
      foreach (var emailContact 
                in assignEmailContacts.Where(a => a.Assigned))
      {                            
        campaign.EmailContacts.Add(
                db.EmailContacts.Find(emailContact.EmailContactId));
            TotalAssigned += 1;
      }
    }
    return (TotalAssigned);
}
Agent Shark

Updating multiple database rows in EF is slow!

I assume there is a table called CampaignContacts containing the data from the n-to-n relationship of Campaign and Contacts. Luckily EF6 allows you to execute raw queries.

using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Text;

public class YourDbContext : DbContext
{
    public void DeleteBatchCampaignContacts(IList<int> ids)
    {
        if (ids == null) return;
        if (ids.Count == 0) return;         

        //ONLY because the parameter comes from a list of Int, else you might risk injection
        Database.ExecuteSqlCommand(string.Format("DELETE FROM CampainContacts WHERE CampaignId in ({0})", string.Join(",", ids)));
    }

    public void UpdateBatchCampaignContacts(int campaignId, IList<int> ids)
    {
        if (ids == null) return;
        if (ids.Count == 0) return;

        Database.ExecuteSqlCommand(string.Format("UPDATE CampaignContacts SET CampaignId = @campaignId WHERE EmailContactId in ({0})", string.Join(",", ids),
            new SqlParameter("@campaignId", campaignId)));
    }
}

Notice the code duplication. You can refactor these functions, but this would be quicker than your current solution. You can add features like executing the query in batches of 50 IDs in order to handle thousands of IDs updates. This is a start.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is best practice for using Unity with Entity Framework in a MVC 4 application

From Dev

MVC4 Code First Entity Framework Upload large files to a SQL Server database

From Dev

Struggling using MVC 5 and Entity Framework 6

From Dev

Entity Framework 4: What is the best way to add and save an entity?

From Dev

Entity Framework 6 - Call a method on removing an entity from the database

From Dev

Populate DropDownList using MVC 4 & Entity Framework

From Dev

How to retrieve image from database without using Entity Framework in ASP.NET MVC4

From Dev

Writing CSV files into Sql database using LinqToCsv - .NET MVC 4 - Entity Framework

From Dev

How to retrieve image from database without using Entity Framework in ASP.NET MVC4

From Dev

Single or multiple DbContext File in mvc4 using Entity framework 6

From Dev

Entity Framework 7 Database First configuration (MVC 6)

From Dev

Entity Framework 7 With Existing Database in .Net 5 MVC 6

From Dev

Entity Framework 6, Database First, MVC5 import view

From Dev

Using Entity Framework 6 class library project with MVC 6

From Dev

Best practice for using entity framework for post deployment database changes

From Dev

Is it proper form to extend a model object (e.g. Product) and add a Create() method that inserts into the database? (MVC 5 Entity Framework 6)

From Dev

What database systems are fully supported by Entity Framework 6 and/or 7?

From Dev

What's the point of an explicit database transaction in Entity Framework 6?

From Dev

Can not find which database is used for MVC 4 with Entity framework

From Dev

Saving images to database with asp.net mvc 4 + Entity Framework

From Dev

MVC4 Entity Framework Controller Database Issue

From Dev

MVC4 Entity Framework 6 Model State Validation Issues

From Dev

What is the best practice to implement advanced search using Entity Framework?

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

Connecting to an Existing Azure Database from an MVC application using Entity Framework

From Dev

Connecting to an Existing Azure Database from an MVC application using Entity Framework

From Dev

How to Fetch Data from database using Entity Framework 6

From Dev

Alter Database in Entity Framework 6

Related Related

  1. 1

    What is best practice for using Unity with Entity Framework in a MVC 4 application

  2. 2

    MVC4 Code First Entity Framework Upload large files to a SQL Server database

  3. 3

    Struggling using MVC 5 and Entity Framework 6

  4. 4

    Entity Framework 4: What is the best way to add and save an entity?

  5. 5

    Entity Framework 6 - Call a method on removing an entity from the database

  6. 6

    Populate DropDownList using MVC 4 & Entity Framework

  7. 7

    How to retrieve image from database without using Entity Framework in ASP.NET MVC4

  8. 8

    Writing CSV files into Sql database using LinqToCsv - .NET MVC 4 - Entity Framework

  9. 9

    How to retrieve image from database without using Entity Framework in ASP.NET MVC4

  10. 10

    Single or multiple DbContext File in mvc4 using Entity framework 6

  11. 11

    Entity Framework 7 Database First configuration (MVC 6)

  12. 12

    Entity Framework 7 With Existing Database in .Net 5 MVC 6

  13. 13

    Entity Framework 6, Database First, MVC5 import view

  14. 14

    Using Entity Framework 6 class library project with MVC 6

  15. 15

    Best practice for using entity framework for post deployment database changes

  16. 16

    Is it proper form to extend a model object (e.g. Product) and add a Create() method that inserts into the database? (MVC 5 Entity Framework 6)

  17. 17

    What database systems are fully supported by Entity Framework 6 and/or 7?

  18. 18

    What's the point of an explicit database transaction in Entity Framework 6?

  19. 19

    Can not find which database is used for MVC 4 with Entity framework

  20. 20

    Saving images to database with asp.net mvc 4 + Entity Framework

  21. 21

    MVC4 Entity Framework Controller Database Issue

  22. 22

    MVC4 Entity Framework 6 Model State Validation Issues

  23. 23

    What is the best practice to implement advanced search using Entity Framework?

  24. 24

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  25. 25

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  26. 26

    Connecting to an Existing Azure Database from an MVC application using Entity Framework

  27. 27

    Connecting to an Existing Azure Database from an MVC application using Entity Framework

  28. 28

    How to Fetch Data from database using Entity Framework 6

  29. 29

    Alter Database in Entity Framework 6

HotTag

Archive