title: How to configure Many to One relationship?
body: <p>Currently I'm working on a Rest api in dotnet core with a Mysql Database. At the moment I have two Tables with Users and roles. For my project I need to get the user and the role. In my code I pecified the relationships as stated in this Sample</p>
  
  <pre><code>public DbSet&lt;User&gt; Users { get; set; }
  private void InitEntityRole(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;Role&gt;(entity =&gt;
      {
           entity.Property(e =&gt; e.Id).ValueGeneratedOnAdd().HasColumnName("Id");
           entity.Property(e =&gt; e.Name).HasColumnName("naam");
      });
  }
  
  private void InitEntityUser(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;User&gt;(entity =&gt;
      {
          entity.Property(e =&gt; e.Id).ValueGeneratedOnAdd().HasColumnName("Id");
          entity.Property(e =&gt; e.Username).HasColumnName("gebruikersnaam");
          entity.Property(e =&gt; e.PasswordHash).HasColumnName("wachtwoord");
          entity.Property(e =&gt; e.PasswordSalt).HasColumnName("salt");
          entity.HasOne(u =&gt; u.Person)
              .WithOne(p =&gt; p.User)
              .HasForeignKey&lt;Person&gt;(p =&gt; p.Id)
              .HasConstraintName("fk_persoon");
          entity.HasOne(u =&gt; u.Role)
              .WithMany(r =&gt; r.Users)
              .HasForeignKey(r =&gt; r.Id)
              .HasConstraintName("fk_gebruikerrol");
        });
  }
  </code></pre>
  
  <p>In my code I have a test function to retrieve all users like this</p>
  
  <pre><code>public List&lt;User&gt; GetAll()
  {
      var users = _dbContext.Users.Include(u =&gt; u.Role).ToList();
      users.ForEach(u =&gt; u.Password = null);
  
      return users;
  }
  </code></pre>
  
  <p>My model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Linq;
  using System.Threading.Tasks;
  using Newtonsoft.Json;
  
  namespace DamstraChallengeBackend.Models
  {
      [Table("rol")]
      public class Role
      {
          public int Id { get; set; }
          public string Name { get; set; }
  
          [JsonIgnore]
          public ICollection&lt;User&gt; Users { get; set; }
      }
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations.Schema;    
  using System.Linq;
  using System.Threading.Tasks;
  using Newtonsoft.Json;
  
  namespace DamstraChallengeBackend.Models
  {
      [Table("gebruiker")]
      public class User
      {
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int Id { get; set; }
          public string Username { get; set; }
          [NotMapped]
          public string Password { get; set; }
          [JsonIgnore]
          public byte[] PasswordHash { get; set; }
          [JsonIgnore]
          public byte[] PasswordSalt { get; set; }
          public Person Person { get; set; }
          public Role Role { get; set; }
  
          [NotMapped]
          [JsonIgnore]
          public string Token { get; set; }
  
      }
  }
  </code></pre>
  
  <p>My tables are configured ass followed </p>
  
  <p>User: </p>
  
  <pre><code>CREATE TABLE `gebruiker` (
     `Id` int(11) NOT NULL AUTO_INCREMENT,
     `gebruikersnaam` varchar(255) NOT NULL,
     `wachtwoord` blob NOT NULL,
     `salt` blob NOT NULL,
     `p_id` int(11) NOT NULL,
     `r_id` int(11) NOT NULL,
      PRIMARY KEY (`Id`),
      KEY `fk_persoon` (`p_id`),
      KEY `fk_gebruikerrol` (`r_id`),
      CONSTRAINT `fk_gebruikerrol` FOREIGN KEY (`r_id`) REFERENCES `rol` (`Id`),
      CONSTRAINT `fk_persoon` FOREIGN KEY (`p_id`) REFERENCES `persoon` (`Id`)
  ) 
  ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 
  COLLATE=utf8mb4_0900_ai_ci
  </code></pre>
  
  <p>Role:</p>
  
  <pre><code>CREATE TABLE `rol` (
      `Id` int(11) NOT NULL AUTO_INCREMENT,
      `naam` varchar(255) NOT NULL,
       PRIMARY KEY (`Id`)
  ) 
  ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 
  COLLATE=utf8mb4_0900_ai_ci
  </code></pre>
  
  <p>When I execute this code, all I get is an empty array. I think that I made an error in the cofiguration but I can't seem to find it. Please could anyone help me.</p>
  
  <p>The things I tried are: Leaving out the include and then I get an array with users without there roles. But as soon as I add the include statement nothing shows up.</p>
  
  <p>The thing I find curious is that it worked when I had one user with a group. As soon as I add more all I get is an empty array.</p>
  
  <p>I apologize for the errors in my English but I hope it's clear enough.</p>