简体   繁体   English

实体框架 - 包括属性,但排除单列

[英]Entity Framework - Include Properties, but exclude single columns

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:Entity Framework中,我可以获得这样的列表:

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.我知道我可以使用 .Select() 并创建一个没有数据属性的 object,但是由于我使用了很多外键,所以我宁愿使用 include。
I also know that it is better to separate the document's metadata from the actual content, but well... I haven't...我也知道最好将文档的元数据与实际内容分开,但是……我还没有……

Make another model that doesnt include data that you dont need制作另一个不包含您不需要的数据的 model

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

And then use this model as a property in primary model:然后将这个 model 用作主要 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)然后只包括想要的数据.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)由于代表数量少而添加为答案(不能发表评论)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM