With the help of a colleague, I did find a solution to keep from having to innumerate all the properties.
I used the AutoMapper package and it works great.
For anyone interested, here is how it works.
- Run this from the command prompt, in the project
server
directory:
dotnet add package AutoMapper
- In your Models > Northwind > Employee.Custom.cs (you have to Ignore Collections):
namespace Northwind.Models.NorthwindDb { public partial class Employee { public string FullName { get; set; } } public class EmployeeProfile: AutoMapper.Profile { public EmployeeProfile() { CreateMap<Employee,Employee>() .ForMember(dest => dest.Employees1, opt => opt.Ignore()) .ForMember(dest => dest.Orders, opt => opt.Ignore()) .ForMember(dest => dest.EmployeeTerritories, opt => opt.Ignore()) .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.FirstName + " " + src.LastName) ); } } }
- In Controllers > NorthwindDB > EmployeeController.Custom.cs:
using System.Linq;
namespace Northwind.Controllers.NorthwindDb { using Northwind.Models.NorthwindDb; using AutoMapper.QueryableExtensions; public partial class EmployeesController { partial void OnEmployeesRead(ref IQueryable<Employee> items) { var config = new AutoMapper.MapperConfiguration(cfg => cfg.AddProfile<EmployeeProfile>()); items = items.ProjectTo<Employee>(config); } } }
- Then, to keep the ModelBuilder happy, here is my Data > NorthwindDBContext.Custom.cs file:
using Microsoft.EntityFrameworkCore; using Northwind.Models.NorthwindDb; namespace Northwind.Data { public partial class NorthwindDbContext { partial void OnModelBuilding(ModelBuilder builder) { builder.Entity<Employee>().Ignore(e => e.FullName); } } }
I think you could also just use [NotMapped] in the Model definition, instead of the last step. Not sure about that as I didn't try it.
Thanks, Dan