Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:06:12+00:00 2026-06-04T03:06:12+00:00

I have been using the Entity Framework with the POCO First approach. I have

  • 0

I have been using the Entity Framework with the POCO First approach. I have pretty much followed the pattern described by Steve Sanderson in his book ‘Pro ASP.NET MVC 3 Framework’, using a DI container and DbContext class to connect to SQL Server.

The underlying tables in SQL server contain very large datasets used by different applications. Because of this I have had to create views for the entities I need in my application:

class RemoteServerContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Contact> Contacts { get; set; }
    ...

    protected override void  OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>().ToTable("vw_Customers");
        modelBuilder.Entity<Order>().ToTable("vw_Orders");
        ...
    }
}

and this seems to work fine for most of my needs.

The problem I have is that some of these views have a great deal of data in them so that when I call something like:

var customers = _repository.Customers().Where(c => c.Location == location).Where(...);

it appears to be bringing back the entire data set, which can take some time before the LINQ query reduces the set to those which I need. This seems very inefficient when the criteria is only applicable to a few records and I am getting the entire data set back from SQL server.

I have tried to work around this by using stored procedures, such as

public IEnumerable<Customer> CustomersThatMatchACriteria(string criteria1, string criteria2, ...) //or an object passed in!
{
    return Database.SqlQuery<Customer>("Exec pp_GetCustomersForCriteria @crit1 = {0}, @crit2 = {1}...", criteria1, criteria2,...);
}

whilst this is much quicker, the problem here is that it doesn’t return a DbSet and so I lose all of the connectivity between my objects, e.g. I can’t reference any associated objects such as orders or contacts even if I include their IDs because the return type is a collection of ‘Customers’ rather than a DbSet of them.

Does anyone have a better way of getting SQL server to do the querying so that I am not passing loads of unused data around?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:06:14+00:00Added an answer on June 4, 2026 at 3:06 am
    var customers = _repository.Customers().Where(c => c.Location == location).Where(...
    

    If Customers() returns IQueryable, this statement alone won’t actually be ‘bringing back’ anything at all – calling Where on an IQueryable gives you another IQueryable, and it’s not until you do something that causes query execution (such as ToList, or FirstOrDefault) that anything will actually be executed and results returned.

    If however this Customers method returns a collection of instantiated objects, then yes, since you are asking for all the objects you’re getting them all.

    I’ve never used either code-first or indeed even then repository pattern, so I don’t know what to advise, other than staying in the realm of IQueryable for as long as possible, and only executing the query once you’ve applied all relevant filters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Entity Framework CTP with Code-First as in this tutorial by
I'm using Entity Framework 4.1 Code First and have been trying to add a
Can anyone help? I have been using the entity framework and its going well
I've been trying to implement POCO over WCF (using Entity Framework 4.0) - just
I have been using Entity Framework 4.3 on an existing database and I have
I have an assembly generated using POCO template using Entity Framework (e.g. Company.Models.dll).Besides generated
Background I have been now using Entity Framework + Linq for about year and
I have been playing with Entity Framework and so far I like using it
I have been using linq in combination with entity framework for a few months
I have been looking into using the Entity Framework in my C# game server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.