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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:06:21+00:00 2026-05-31T02:06:21+00:00

In EF CodeFirst Fluent API I can write something like this: modelBuilder.Entity<MyEntity>() .Ignore(e =>

  • 0

In EF CodeFirst Fluent API I can write something like this:

modelBuilder.Entity<MyEntity>()
            .Ignore(e => e.Property1);
modelBuilder.Entity<MyEntity>()
            .Ignore(e => e.Property2);

How to ignore all properites but a tiny set, like this:

modelBuilder.Entity<MyEntity>()
            .IgnoreAllBut(e => e.ID, e => e.Important);

Is it possible to write extension method like this IgnoreAllBut?

  • 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-31T02:06:22+00:00Added an answer on May 31, 2026 at 2:06 am

    This is draft, but works:

    public static void IgnoreAllBut<TStructuralType>(
        this StructuralTypeConfiguration<TStructuralType> configuration,
        params Expression<Func<TStructuralType, object>>[] includes)
        where TStructuralType : class
    {
        var includeMembers = ConvertExpressionsToMembers(includes);
        var type = typeof(TStructuralType);
        var properties = type.GetProperties();
        var typeParameter = Expression.Parameter(type);
        var configurationType = configuration.GetType();
        var ignoreMethod = configurationType.GetMethod("Ignore");
        foreach (var property in properties)
        {
            if (!includeMembers.Any(member => member.Name == property.Name))
            {
                var propertyExpression = Expression.Property(typeParameter, property);
                var lambdaExpression = Expression.Lambda(propertyExpression, typeParameter);
                var genericIgnoreMethod = ignoreMethod.MakeGenericMethod(property.PropertyType);
                var parameters = new object[] { lambdaExpression };
                genericIgnoreMethod.Invoke(configuration, parameters);
            }
        }
    }
    
    private static List<MemberInfo> ConvertExpressionsToMembers<TStructuralType>(
        Expression<Func<TStructuralType, object>>[] expressions)
    {
        var members = new List<MemberInfo>();
        foreach (var lambda in expressions)
        {
            var expression = lambda.Body;
            var memberExpression = expression as MemberExpression;
            if (memberExpression == null)
            {
                var unaryExpression = expression as UnaryExpression;
                memberExpression = unaryExpression.Operand as MemberExpression;
            }
            members.Add(memberExpression.Member);
        }
        return members;
    }
    

    Thanks to DarkGray for code in linked question.

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

Sidebar

Related Questions

This is probably just because my knowledge with the EF Code First fluent API
I'll let the code do the talking here, I have something like this: class
I use EntityFramework CodeFirst MVC3. In my model I defined a nullable property like
I created a simple CodeFirst (Entity Framework 4.1) context and then, using the right
I am using Entity Framework CodeFirst where I have used Parent Child relations using
I have a webforms project using EF codefirst to persist data. I'd like to
I'm having a problem with the Entity Framework (EF4.1 - CodeFirst) It's basically duplicating
I need to represent a composite pattern with Entity Framework code first. How can
So I have been struggling with this one all morning. I have read a
I'm using EF 4.1 Code First, with a Fluent Mapping: Entity: public class MyClass

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.