0

I been breaking my head for hours trying to find out why my navigation property is coming back null for 0 to many scenario. For 1 to 1 it works just fine.

These are my entities:

public class Barber: BaseEntity
{
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public int? AppointmentId { get; set; }

    public int? UserId { get; set; }
    public  virtual ICollection<Users> Users { get; set; }

    public Barber()
    {
    }
}

public class Users: BaseEntity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public bool IsBarber { get; set; }
    public int? BarberId { get; set; }
    public string FacebookToken { get; set; }
    public DateTime? CreatedOn { get;  set;}

    public virtual Barber Barber { get; set; }

    /// <summary>
    /// default constructor
    /// </summary>
    public Users() { }
}

my mapping is as follow: for Barber:

    public BarberMapping()
    {
        ToTable("Barber");

        HasKey(p => p.Id);
        Property(p => p.Street1).HasColumnName("Street1");
        Property(p => p.Street2).HasColumnName("Street2");
        Property(p => p.City).HasColumnName("City");
        Property(p => p.State).HasColumnName("State");
        Property(p => p.PostalCode).HasColumnName("PostalCode");
        Property(p => p.AppointmentId).HasColumnName("AppointmentId");

        Property(p => p.UserId).HasColumnName("UserId");

        HasOptional(p => p.Users)
            .WithMany()
            .HasForeignKey(p => p.UserId);
    }
}

for User:

    public UserMapping()
    {
        ToTable("Users");

        HasKey(p => p.Id);

        Property(p => p.FirstName);
        Property(p => p.LastName  );
        Property(p => p.Password);
        Property(p => p.Email);
        Property(p => p.CreatedOn);
        Property(p => p.FacebookToken);
        Property(p => p.IsBarber);

    }

Finally i'm calling my data like this:

_userRepository.All.Where(p => p.Id == id).Include(p => p.Barber).FirstOrDefault();

When the query comes back I get everything except the Barber property which comes back as null. Anything i'm missing to make this relationShip work?

I'm using EF 6.1.3 with mySQL 6.9 on visual studio for mac.

savage305
  • 45
  • 4
  • The `UserId` FK property along with `Users` collection navigation property in `Barber` looks confusing. What type of relationship(s) between `User` and `Barber` are you trying to model? – Ivan Stoev Oct 12 '17 at 08:35
  • A user could’ve have a barber or it might not, but a barber might have at many users – savage305 Oct 12 '17 at 10:50
  • 1
    The collection side (Barber) doesn't need a FK to user. See [this example](http://www.entityframeworktutorial.net/entity-relationships.aspx) where Standard = Barber and Teacher = User. Also [here](https://stackoverflow.com/questions/25895479/how-to-set-a-0-relationship-in-entity-framework-code-first) – Steve Greene Oct 12 '17 at 13:31

0 Answers0