简体   繁体   中英

The entity or > complex type 'ChildAccessory' cannot be constructed in a LINQ to > Entities query

I think I understand the following error but I am not sure how to fix it,

"ClassName": "System.NotSupportedException", "Message": "The entity or complex type 'ChildAccessory' cannot be constructed in a LINQ to Entities query.",

Should I create a DTO and return it from repository to fix the issue or there is any better fix?

I am trying to get child accessories but also include default accessories which are in parent table.

public class ChildAccessoryRepository: IChildAccessoryRepository {
 private readonly EquipmentContext _context;

 public DirectoryEquipmentRepository(EquipmentContext context) {
  _context = context;
 }

 public IEnumerable < ChildAccessory > GetChildAccessories(Guid clientReference) {
  var accessories = from a in _context.Accessory
  join ca in _context.ChildAccessory
  on new {
   AccessoryId = a.Id, ClientReference = clientReference
  }
  equals new {
   ca.AccessoryId, ca.ClientReference
  }
  into ca_join
  from ca in ca_join.DefaultIfEmpty()
  where
  ca.IsActive == true ||
   ca.IsActive == null
  //select ca;
                         select new ChildAccessory
                                   {
                                       AccessoryId = ca.AccessoryId,
                                       SomeOtherColumn = ca.SomeOtherColumn,
                                       Accessory = ca.Accessory,
                                   });

 }
}

Does ChildAccessory have a parameterless constructor?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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