0

Let's say I have a list of items, which each can reference a list of documents (and other stuff). In Entity Framework I can get the list like that:

var items = context.Items
        .Include(i => i.Documents)
        .Include(i => i.OtherProperty)
        .ToList()

Now this includes all columns from documents. Is it possible to exclude one of these columns (eg. the "Data"-column which stores the complete document as binary)?
Something like this:

var items = context.Items
        .Include(i => i.Documents)
            .ButExclude(d => d.Data)
        .Include(i => i.OtherProperty)
        .ToList()

I know that I could use .Select() and create an object without the data property, but since I use a lot of foreign keys I would rather like to use includes.
I also know that it is better to separate the document's metadata from the actual content, but well... I haven't...

2
  • 1
    What about creating a 2nd entity for the same table and mapping the Data as Ignore? I would make sure any relationships are unidirectional on one of the 2 entities for that table. Commented Apr 29, 2022 at 13:54
  • Use Automapper or mapster or somthing else with DTO? Commented Apr 29, 2022 at 14:53

1 Answer 1

0

Make another model that doesnt include data that you dont need

public class CreateAndUpdatePropertiesModel
{
    public string Documents { get; set; }
    public string OtherProperties { get; set; }
}

And then use this model as a property in primary model:

public class ExampleModel
{
    public string Documents { get; set; }
    public string Data { get; set; }
    public string OtherProperties { get; set; }

    // And then add the CreateAndUpdateProperties model as a property
    public CreateAndUpdateProperties { get; set; }
}

then only Include wanted data .Include(s => s.CreateAndUpdateProperties)

This Question was previously asked many times:

Exclude columns getting data with Entity Framework

  • Added as answer due to low amount of rep(cant comment)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.