-
Type: Bug
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: 2.10.4
-
Environment:Platform: .net core 3.1
Driver version: 2.10.4
i have db model that contains all field of DTO for object.
For example, if we have entity Foo and it can be shown in table FooTableItemDto
FooTableItemDto { public Guid Id { get; set; } public DateTime CreatedAt { get; set; } public string Creator { get; set; } public int TotalCost { get; set; } }
as card FooViewDto:
FooViewDto { public Guid Id { get; set; } public DateTime CreatedAt { get; set; } public string Creator { get; set; } public Guid CreatorId { get; set; } public FooMaterialDto Material { get; set; } public int TotalCost { get; set; } } FooMaterialDto { public Guid Id { get; set; } public string MaterialName { get; set; } public Guid MaterialId { get; set; } public decimal Amount { get; set; } public decimal Price { get; set; } }
in editor FooEditorDto
FooEditorDto { public Guid Id { get; set; } public FooMaterialDto Material { get; set; } }
then we have all fields from this DTOs in db:
Foo { public Guid Id { get; set; } public DateTime CreatedAt { get; set; } public string Creator { get; set; } public Guid CreatorId { get; set; } public FooMateriall Material { get; set; } public int TotalCost { get; set; } } FooMateriall { public Guid Id { get; set; } public string MaterialName { get; set; } public Guid MaterialId { get; set; } public decimal Amount { get; set; } public decimal Price { get; set; } }
now i have projector factory that build corresponding Expression. For example, if i want to get FooViewDto then factory generate me following expression:
(Foo x) => new FooViewDto { Id = x.Id, CreatedAt = x.CreatedAt, Creator = x.Creator, CreatorId = x.CreatorId, Material = new FooMaterialDto { Id = x.Material.Id, MaterialName = x.Material.MaterialName, MaterialId = x.Material.MaterialId, Amount = x.Material.Amount, Price = x.Material.Price }, TotalCost = x.TotalCost }
and this works fine!
but now, i have case then Material can be null (user can create draft of document without filling this field), but my code does not failed with NullReferenceException but create empty object (with null or default values of all fields)
but i need that Material also be null in this case, so i add IIF expression - check if Material is null in db and return null in such case, otherwise create projection:
(Foo x) => new FooViewDto { Id = x.Id, CreatedAt = x.CreatedAt, Creator = x.Creator, CreatorId = x.CreatorId, Material = IIF(x.Material == null), null, new FooMaterialDto { Id = x.Material.Id, MaterialName = x.Material.MaterialName, MaterialId = x.Material.MaterialId, Amount = x.Material.Amount, Price = x.Material.Price }, TotalCost = x.TotalCost }
but in this case i got exception during projection:
Element 'Id' does not match any field or property of class FooMaterialDto
the exception was gone if i add explicit mapping for FooMaterialDto. But i don't want to do that, because i suggest that must have mapping only for my db entities, not for dto. and i don't have such things in case if i don't have IIF operation in my expression
so my question is - what i do wrong or missed?
- duplicates
-
CSHARP-3235 FormatException when using AutoMapper.ProjectTo
- Closed
-
CSHARP-1771 Support IIF method (i.e. ternary operator) in LINQ
- Closed