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 5977511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:22:52+00:00 2026-05-22T21:22:52+00:00

Consider the following code, which is calling against an EF generated data context: var

  • 0

Consider the following code, which is calling against an EF generated data context:

var context = new DataContext();
var employees = context.Employees.Include("Department");

If I change the name of the Department relationship then this code is going to start throwing a runtime error. So is there any way to call the .Include() method in a safe manner, so I get compile time checking for all the relationships being referenced?

  • 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-05-22T21:22:53+00:00Added an answer on May 22, 2026 at 9:22 pm

    Taking moi_meme’s idea a step further, my colleague developed the following solution that works in all cases. He introduced a new method caled Includes() for dealing with one-to-many and many-to-many relationships. It allows you to write this:

    context.Customer
        .Include("Address")
        .Include("Orders")
        .Include("Orders.OrderLines")
    

    as this:

    context.Customer
        .Include(c => c.Address)
        .Includes(c => c.Include(customer => customer.Orders)
                        .Include(order => order.OrderLines))
    

    All credit goes to https://stackoverflow.com/users/70427/bojan-resnik, so go give him some love if you like the solution.

    public static class ObjectQueryExtensions
    {
        public static ObjectQuery<T> Includes<T>(this ObjectQuery<T> query, Action<IncludeObjectQuery<T, T>> action)
        {
            var sb = new StringBuilder();
            var queryBuilder = new IncludeObjectQuery<T, T>(query, sb);
            action(queryBuilder);
            return queryBuilder.Query;
        }
    
        public static ObjectQuery<TEntity> Include<TEntity, TProperty>(this ObjectQuery<TEntity> query, Expression<Func<TEntity, TProperty>> expression)
        {
            var sb = new StringBuilder();
            return IncludeAllLevels(expression, sb, query);
        }
    
        static ObjectQuery<TQuery> IncludeAllLevels<TEntity, TProperty, TQuery>(Expression<Func<TEntity, TProperty>> expression, StringBuilder sb, ObjectQuery<TQuery> query)
        {
            foreach (var name in expression.GetPropertyLevels())
            {
                sb.Append(name);
                query = query.Include(sb.ToString());
                Debug.WriteLine(string.Format("Include(\"{0}\")", sb));
                sb.Append('.');
            }
            return query;
        }
    
        static IEnumerable<string> GetPropertyLevels<TClass, TProperty>(this Expression<Func<TClass, TProperty>> expression)
        {
            var namesInReverse = new List<string>();
    
            var unaryExpression = expression as UnaryExpression;
            var body = unaryExpression != null ? unaryExpression.Operand : expression.Body;
    
            while (body != null)
            {
                var memberExpression = body as MemberExpression;
                if (memberExpression == null)
                    break;
    
                namesInReverse.Add(memberExpression.Member.Name);
                body = memberExpression.Expression;
            }
    
            namesInReverse.Reverse();
            return namesInReverse;
        }
    
        public class IncludeObjectQuery<TQuery, T>
        {
            readonly StringBuilder _pathBuilder;
            public ObjectQuery<TQuery> Query { get; private set; }
    
            public IncludeObjectQuery(ObjectQuery<TQuery> query, StringBuilder builder)
            {
                _pathBuilder = builder;
                Query = query;
            }
    
            public IncludeObjectQuery<TQuery, U> Include<U>(Expression<Func<T, U>> expression)
            {
                Query = ObjectQueryExtensions.IncludeAllLevels(expression, _pathBuilder, Query);
                return new IncludeObjectQuery<TQuery, U>(Query, _pathBuilder);
            }
    
            public IncludeObjectQuery<TQuery, U> Include<U>(Expression<Func<T, EntityCollection<U>>> expression) where U : class
            {
                Query = ObjectQueryExtensions.IncludeAllLevels(expression, _pathBuilder, Query);
                return new IncludeObjectQuery<TQuery, U>(Query, _pathBuilder);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code which shows compile time error : #include <stdio.h> int main(int
Consider the following code: string propertyName; var dateList = new List<DateTime>() { DateTime.Now };
Consider the following code which is typcial of many ChannelFactory examples: WSHttpBinding myBinding =
consider the following code, which represents an attempt to implement partial matching. the intended
Let's consider the following scenario: a function which can generate code colors from white
Consider the following code: client.Send(data, data.Length, endpoint); byte[] response = client.Receive(ref endpoint); While, according
Consider the following code which is to be thrown at an AR find: conditions
I've got some basic questions about C++. Consider the following code in which I
I have a relatively simple use case which is failing. Consider the following code:
Consider the following sample code which uses a TrustManager to log whether an outgoing

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.