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

  • Home
  • SEARCH
  • 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 7770985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:32:19+00:00 2026-06-01T16:32:19+00:00

I am working on making an entity framework project more scalable to suit our

  • 0

I am working on making an entity framework project more scalable to suit our requirements. The project uses an EDMX file, Entity Framework, and .NET 4.0. The project has globally setup lazy loading.

Basically, I have 2 tables:

Product
  ProductId

ProductVariant
  ProductVariantId
  ProductId

I have a situation where there will be hundreds of thousands of related rows in ProductVariant for a given Product. So I would like to remove the navigation property for ProductVariant and make a query to lookup only the most pertainant data, rather than all of the related data.

However, most of the code does something like:

if (Product.HasVariants) { [Loop Through Variants And Do Something] }

Which I will change to:

if (Product.HasVariants) { [Lookup Subset of Variants from DB And Do Something] }

HasVariants is a method that currently looks like this:

public bool HasVariants()
{
    return this.Variants.Count > 0;
}

Variants is a navigation property. What I would like to do is create a lookup query with a field indicating whether any related data exists in the variants table rather than calling the database every time the code does this check.

This is how I would accomplish this in a stored procedure:

CREATE Procedure GetProduct
  @ProductId
AS

Declare @HasVariants bit

Set @HasVariants = EXISTS(Select ProductVariantId 
                          From ProductVariant 
                          Where ProductId = @ProductId)

SELECT ProductId, <Other Fields>, @HasVariants AS HasVariants
FROM Product
WHERE ProductID = @ProductID

Return

Note the presence of the HasVariants field, which I would use in the HasVariants method instead of the collection.

My question is how do I accomplish the same thing (a lookup field from a related table) using an EDMX based model? I want to do this without using SQL statements in the code, but if the solution involves making HasVariants into a navigation property (possible?) it is acceptable. I already understand how to write a query to execute on demand, what I am looking for is a way to make the entity framework cache the HasVariants property the same way it does for the rest of the product data.

Forgive me if I didn’t provide enough information. I am new to the Entity Framework and I am really not sure what is needed to answer this question.

  • 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-01T16:32:21+00:00Added an answer on June 1, 2026 at 4:32 pm

    If you want to duplicate your stored procedure you will need to create a new class and implement something like this:

    public IQueryable<ProductWithHasVariant> GetProduct(DBContext context, int productId)
    {
      return from p in context.Products
             where p.ProductId == productId
             select new ProductWithHasVariant
             {
               p.ProductId,
               HasVariants = p.Variants.Any(),
               .
               .
               .
             };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project with MySQL,Hibernate,Java,GWT I am making an entity class
I'm working on a personal project using WPF with Entity Framework and Self Tracking
Im thinking of making a custom datatypes / prototypes for a project im working
I am currently working on making my code contain more generics. I encountered a
I am working on a project and I would like to send an entity
I am working on making an existing iPhone/iPad project backwards compatible down to iPhoneOS
I'm working on making an interface for a robot. My Robot class has methods
I'm working on making a simple server application with python, and I'm trying to
I'm working on making 3 VM versions of XP with IE6, 7, and 8
I'm working on making an ASCII based game, and everywhere I look people are

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.