EF Core LINQ exclude column from included entity

百般思念 提交于 2020-04-07 06:38:52

问题


I'm using ASP.Net Core 2.0 with Entity Framework and I am trying to return a model to a page that contains the Employment entity with it's collection of EmploymentDocument entities also included. For the latter I do not want to load the data (byte[]) column but I do want all the other columns, most importantly FileName.

The linq query I have which loads everything including the data column is:

var employment = await _context.Employment
    .Include(e => e.EmploymentDocuments) // will load all associated document data
    .SingleOrDefaultAsync(m => m.EmploymentID == id);

The purpose here is to be able to show a list of all the document names on the page with links that can then be used to download the data for the file selected.


回答1:


Select all data you need by hands and store it in some Dto object:

var employment = await _context.Employment
    .Where(m => m.EmploymentID == id)
    .Select(e => new EmploymentDto
    { 
        ID = e.EmploymentID,
        Docs = e.EmploymentDocuments.Select(o => o.FileName)
    })
    .SingleOrDefaultAsync();


来源:https://stackoverflow.com/questions/47300555/ef-core-linq-exclude-column-from-included-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!