C
C#9mo ago
kurumi

✅ AutoMapper record to class problems

I need to create map from
public record MessageModel(
int MessageId,
int AuthorId,
string Text,
IEnumerable<int> Likes,
DateTime SendDate);
public record MessageModel(
int MessageId,
int AuthorId,
string Text,
IEnumerable<int> Likes,
DateTime SendDate);
to:
public class MessageEntity
{
public int Id { get; set; }
public int AuthorId { get; set; }
public UserEntity? Author { get; set; }
public string Text { get; set; } = string.Empty;
public DateTime SendDate { get; set; }
public List<MessageLikeEntity> Likes { get; set; } = new List<MessageLikeEntity>();
}
public class MessageEntity
{
public int Id { get; set; }
public int AuthorId { get; set; }
public UserEntity? Author { get; set; }
public string Text { get; set; } = string.Empty;
public DateTime SendDate { get; set; }
public List<MessageLikeEntity> Likes { get; set; } = new List<MessageLikeEntity>();
}
I quickly made it:
CreateMap<MessageModel, MessageEntity>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.MessageId));
CreateMap<MessageModel, MessageEntity>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.MessageId));
So I want keep Id, AuthorId, Text and SendDate be mapped But it throws exception: AutoMapper.AutoMapperMappingException Message=Error mapping types. Source=AutoMapper Inner Exception 1: AutoMapperMappingException: Missing type map configuration or unsupported mapping. So what I did wrong? Surprising, this maps fine:
public record UserModel(
int UserId,
string Name,
UserGender Gender,
DateTime DateOfBirth,
DateTime LastVisit,
bool Online);

public class UserEntity
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public UserGender Gender { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime LastVisit { get; set; }
public bool Online { get; set; }
public List<FriendRequestEntity> SendFriendRequests { get; set; } = new List<FriendRequestEntity>();
public List<FriendRequestEntity> ReceivedFriendRequests { get; set; } = new List<FriendRequestEntity>();
public List<MessageEntity> Messages { get; set; } = new List<MessageEntity>();
public List<MessageLikeEntity> MessageLikes { get; set; } = new List<MessageLikeEntity>();
}

CreateMap<UserModel, UserEntity>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.UserId));
public record UserModel(
int UserId,
string Name,
UserGender Gender,
DateTime DateOfBirth,
DateTime LastVisit,
bool Online);

public class UserEntity
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public UserGender Gender { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime LastVisit { get; set; }
public bool Online { get; set; }
public List<FriendRequestEntity> SendFriendRequests { get; set; } = new List<FriendRequestEntity>();
public List<FriendRequestEntity> ReceivedFriendRequests { get; set; } = new List<FriendRequestEntity>();
public List<MessageEntity> Messages { get; set; } = new List<MessageEntity>();
public List<MessageLikeEntity> MessageLikes { get; set; } = new List<MessageLikeEntity>();
}

CreateMap<UserModel, UserEntity>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.UserId));
6 Replies
Pobiega
Pobiega9mo ago
Are you mapping from a DTO to a database entity?
kurumi
kurumiOP9mo ago
Yes
Pobiega
Pobiega9mo ago
Thats generally not a great idea - since your nav props etc will all be empty.
kurumi
kurumiOP9mo ago
Hm, so what is the best practice?
Pobiega
Pobiega9mo ago
you'll need to add ignores to all unmapped properties for the destination, and the Likes property is causing issues since it has different types well the only possible scenario where mapping from a dto to an entity would make any sense would be for creating a new from scratch but at that point you dont have any IDs usually better to just have a CreateXRequest and create the entity based on the info in that, manually. Mapping is mostly done from entity->dto for representation purposes, or even better, with a .ProjectTo Queryable extension
kurumi
kurumiOP9mo ago
Hah, the problem is I need to pass huge json file which structure wasn't designed to be good for database Any way thx, Ill ignore all properties that can be mapped - it helps a bit 😄
Want results from more Discord servers?
Add your server