Search code examples
linqentity-framework-coreautomapper.net-8.0

Context.Items are only available when using a Map overload that takes Action<IMappingOperationOptions>! Consider using Context.TryGetItems instead


I'm getting this error after upgrade from .NET 6 to .NET 8:

Context.Items are only available when using a Map overload that takes Action(IMappingOperationOptions)! Consider using Context.TryGetItems instead.

when mapping entity to model through automapper. here i have mention mapper profile from entity to model and also added entity and model for reference

Code:

_mapper.Map<List<UserRoleMapping>, List<UserRoleMappingModel>>(user.UserRoleMappings); getting error on this line
  • UserRoleMapping is an entity
  • UserRoleMappingModel is a model class
  • user.UserRoleMappings is context result

Mapper profile

CreateMap<UserRoleMapping, UserRoleMappingModel>()
     .ForMember(m => m.UserRoleName, o => o.MapFrom(src => src.UserRole.Name))
     .ForMember(m => m.UserName, o => o.MapFrom(src => src.User.Name))
     .ForMember(m => m.Guid, o => o.MapFrom(src => src.UserRole.Guid.ToString()));

Enitity UserRoleMapping

public class UserRoleMapping : AbstractEntity
{
    public virtual int UserRoleId { get; set; }
    
    public virtual UserRole UserRole { get; set; }
    
    public virtual int UserId { get; set; }
    
    public virtual User User { get; set; }
    
    public virtual bool isReadOnly { get; set; }
    public virtual bool isReadWrite { get; set; }
    public virtual bool isAdmin { get; set; }
    public virtual bool IsEnterpriseAccess { get; set; }
}

UserRoleMappingModel

public class UserRoleMappingModel
{
  public int UserRoleId { get; set; }
    
    public string UserRoleName { get; set; }
    
    public int UserId { get; set; }
    
    public string UserName { get; set; }
    
    public string Guid { get; set; }
    
    public bool isReadOnly { get; set; }
    
    public bool isReadWrite { get; set; }
    
    public bool isAdmin { get; set; }
    
    public bool IsEnterpriseAccess { get; set; }
}

Previously it was working with .net6 i have updated all the package of automapper and efcore to latest release automapper 13.0.1 EfCore 8.0.6


Solution

  • Tough to say without seeing the properties in the Entity and the Model but you can try this:

    var userRoleMappingsModel = _mapper.Map<List<UserRoleMappingModel>>(user.UserRoleMappings);