问题
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