locked
Error projecting and mapping with EF Core RRS feed

  • Question

  • User-1142747527 posted

    I have two models:

    [Table("users")]
    public class User
    {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("user_id")]
    public int? UserId { get; set; }
    [Column("name_identifier")]
    public string NameIdentifier { get; set; }
    [Column("first_name")]
    public string FirstName { get; set; }
    [Column("last_name")]
    public string LastName { get; set; }
    [Column("email")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [DataType(DataType.PhoneNumber)]
    [Column("phone_number")]
    public string PhoneNumber { get; set; }
    [Column("lockout_enabled")]
    public bool LockoutEnabled { get; set; }
    
    [Column("group_id")]
    public int? GroupId { get; set; }
    [ForeignKey("GroupId")]
    public Group Group { get; set; }
    
    public int? AddedById { get; set; }
    [ForeignKey("AddedById")]
    public virtual User AddedBy { get; set; }
    [Column("added_date")]
    public DateTime? AddedDate { get; set; }
    public int? DeletedById { get; set; }
    [ForeignKey("DeletedById")]
    public virtual User DeletedBy { get; set; }
    [Column("deleted_date")]
    public DateTime? DeletedDate { get; set; }
    }

    and

    public class UserDto
    {
    public int? UserId { get; set; } 
    public string NameIdentifier { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public bool LockoutEnabled { get; set; }
    public string PhoneNumber { get; set; }
    public int? GroupId { get; set; }
    public DateTime? AddedDate { get; set; }
    public DateTime? DeletedDate { get; set; }
    }

    I then have a controller method in my web API which performs a projection in the database (MySQL - using Pomelo) and maps it from the User collection to a UserDto collection:

    // GET: api/Users
    [HttpGet]
    [Authorize]
    public IEnumerable<UserDto> GetUser()
    {
        IEnumerable<UserDto> userDtos = _context.User
        .Select(u => new UserDto
        {
            UserId = u.UserId,
            GroupId = u.GroupId,
            AddedDate = u.AddedDate,
            DeletedDate = u.DeletedDate,
            Email = u.Email,
            PhoneNumber = u.PhoneNumber,
            FirstName = u.FirstName,
            LastName = u.LastName,
            LockoutEnabled = u.LockoutEnabled,
            NameIdentifier = u.NameIdentifier
        });
        return userDtos;
    }


    But it gives me this error:

    System.InvalidOperationException: 'Unable to determine the relationship represented by navigation property 'Group.AddedBy' of type 'User'. Either manually configure the relationship, or ignore this property from the model.'

    But I can't figure this out, because it should ignore the Group property altogether, shouldnt it? I'm not interested in the Group field - my UserDto doesn't even have a Group property.

    For the record, without projection/mapping, this:

    IEnumerable<User> users = _context.User



    works perfectly. Any ideas? Thank you...

    Tuesday, June 13, 2017 8:57 AM

Answers

  • User-1142747527 posted

    Found the problem - the issue was that i was missing a Column name mapping in my User object:

    [Column("added_by_id")]
    public int? AddedById { get; set; }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 13, 2017 9:41 AM