title: Returning Null for the Related Entity
body: <p>I am using EF core 2.1.14
  Here is my Class for DbContext which I have created by scaffolding:</p>
  
  <pre><code>public partial class AgriDbContext : DbContext
  {
      public AgriDbContext()
      {
      }
  
      public AgriDbContext(string connectionString)
          : base(GetOptions(connectionString))
      {
      }
      private static DbContextOptions GetOptions(string connectionString)
      {
          return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
      }
      public virtual DbSet&lt;Advertisement&gt; Advertisements { get; set; }
      public virtual DbSet&lt;AgroItem&gt; AgroItems { get; set; }
      public virtual DbSet&lt;BuyerAddsDifferentAdsToFav&gt; BuyerAddsDifferentAdsToFavs { get; set; }
      public virtual DbSet&lt;BuyersAddAgroItemToInterest&gt; BuyersAddAgroItemToInterests { get; set; }
      public virtual DbSet&lt;Category&gt; Categories { get; set; }
      public virtual DbSet&lt;City&gt; Cities { get; set; }
      public virtual DbSet&lt;SellersFavoritesBuyer&gt; SellersFavoritesBuyers { get; set; }
      public virtual DbSet&lt;User&gt; Users { get; set; }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.HasAnnotation("ProductVersion", "2.2.0-rtm-35687");
  
          modelBuilder.Entity&lt;Advertisement&gt;(entity =&gt;
          {
              entity.Property(e =&gt; e.Picture).IsUnicode(false);
  
              entity.HasOne(d =&gt; d.City)
                  .WithMany(p =&gt; p.Advertisements)
                  .HasForeignKey(d =&gt; d.CityId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("ADVERTISEMENTS_INCLUDE_A_CITY");
  
              entity.HasOne(d =&gt; d.Item)
                  .WithMany(p =&gt; p.Advertisements)
                  .HasForeignKey(d =&gt; d.ItemId)
                  .HasConstraintName("AN_ADVERTISEMENT_IS_RELATED_TO_AN_ITEM");
  
              entity.HasOne(d =&gt; d.Seller)
                  .WithMany(p =&gt; p.Advertisements)
                  .HasForeignKey(d =&gt; d.SellerId)
                  .HasConstraintName("USERS_POST_ADVERTISEMENTS");
          });
  
          modelBuilder.Entity&lt;AgroItem&gt;(entity =&gt;
          {
              entity.Property(e =&gt; e.Name).IsUnicode(false);
  
              entity.Property(e =&gt; e.Uname).IsUnicode(false);
  
              entity.HasOne(d =&gt; d.Category)
                  .WithMany(p =&gt; p.AgroItems)
                  .HasForeignKey(d =&gt; d.CategoryId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("AGRO_ITEMS_BELONG_TO_A_CATEGORY");
          });
  
          modelBuilder.Entity&lt;BuyerAddsDifferentAdsToFav&gt;(entity =&gt;
          {
              entity.HasKey(e =&gt; new { e.BuyerId, e.AdId });
  
              entity.HasOne(d =&gt; d.Ad)
                  .WithMany(p =&gt; p.BuyerAddsDifferentAdsToFavs)
                  .HasForeignKey(d =&gt; d.AdId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("FK_BUYER_ADDS_DIFFERENT_ADS_TO_FAV_ADVERTISEMENTS");
  
              entity.HasOne(d =&gt; d.Buyer)
                  .WithMany(p =&gt; p.BuyerAddsDifferentAdsToFavs)
                  .HasForeignKey(d =&gt; d.BuyerId)
                  .HasConstraintName("FK_BUYER_ADDS_DIFFERENT_ADS_TO_FAV_USERS");
          });
  
          modelBuilder.Entity&lt;BuyersAddAgroItemToInterest&gt;(entity =&gt;
          {
              entity.HasKey(e =&gt; new { e.BuyerId, e.ItemId });
  
              entity.HasOne(d =&gt; d.Buyer)
                  .WithMany(p =&gt; p.BuyersAddAgroItemToInterests)
                  .HasForeignKey(d =&gt; d.BuyerId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("FK_BUYERS_ADD_AGRO_ITEM_TO_INTEREST_USERS");
  
              entity.HasOne(d =&gt; d.Item)
                  .WithMany(p =&gt; p.BuyersAddAgroItemToInterests)
                  .HasForeignKey(d =&gt; d.ItemId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("FK_BUYERS_ADD_AGRO_ITEM_TO_INTEREST_AGRO_ITEMS");
          });
  
          modelBuilder.Entity&lt;Category&gt;(entity =&gt;
          {
              entity.Property(e =&gt; e.Name).IsUnicode(false);
  
              entity.Property(e =&gt; e.Uname).IsUnicode(false);
          });
  
          modelBuilder.Entity&lt;City&gt;(entity =&gt;
          {
              entity.HasIndex(e =&gt; e.Id)
                  .HasName("UNIQUE_LOCATION")
                  .IsUnique();
  
              entity.Property(e =&gt; e.Name).IsUnicode(false);
          });
  
          modelBuilder.Entity&lt;SellersFavoritesBuyer&gt;(entity =&gt;
          {
              entity.HasKey(e =&gt; new { e.SellerId, e.BuyerId });
  
              entity.HasOne(d =&gt; d.Buyer)
                  .WithMany(p =&gt; p.SellersFavoritesBuyerBuyers)
                  .HasForeignKey(d =&gt; d.BuyerId)
                  .HasConstraintName("FK_SELLERS_FAVORITES_BUYERS_USERS1");
  
              entity.HasOne(d =&gt; d.Seller)
                  .WithMany(p =&gt; p.SellersFavoritesBuyerSellers)
                  .HasForeignKey(d =&gt; d.SellerId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("FK_SELLERS_FAVORITES_BUYERS_USERS");
          });
  
          modelBuilder.Entity&lt;User&gt;(entity =&gt;
          {
              entity.HasIndex(e =&gt; new { e.CcompanyCode, e.CcountryCode, e.Cphone })
                  .HasName("UNIQUE_CONTACT")
                  .IsUnique();
  
              entity.Property(e =&gt; e.Address).IsUnicode(false);
  
              entity.Property(e =&gt; e.Fname).IsUnicode(false);
  
              entity.Property(e =&gt; e.Lname).IsUnicode(false);
  
              entity.HasOne(d =&gt; d.City)
                  .WithMany(p =&gt; p.Users)
                  .HasForeignKey(d =&gt; d.CityId)
                  .OnDelete(DeleteBehavior.ClientSetNull)
                  .HasConstraintName("FK_USERS_CITIES");
          });
  
          OnModelCreatingPartial(modelBuilder);
      }
  
      partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
  }
  </code></pre>
  
  <p>In the above code CityId is properly Related as a foreign Key and there is also a property for City in the User class. But whenever I get a user it returns null in the City Property but it returns CityId from the database.</p>
  
  <p>Below is my Entity Model Class</p>
  
  <pre><code>[Table("USERS")]
  public partial class User
  {
      public User()
      {
          Advertisements = new HashSet&lt;Advertisement&gt;();
          BuyerAddsDifferentAdsToFavs = new HashSet&lt;BuyerAddsDifferentAdsToFav&gt;();
          BuyersAddAgroItemToInterests = new HashSet&lt;BuyersAddAgroItemToInterest&gt;();
          SellersFavoritesBuyerBuyers = new HashSet&lt;SellersFavoritesBuyer&gt;();
          SellersFavoritesBuyerSellers = new HashSet&lt;SellersFavoritesBuyer&gt;();
      }
  
      public long Id { get; set; }
      [Required]
      [Column("FName")]
      public string Fname { get; set; }
      [Required]
      [Column("LName")]
      public string Lname { get; set; }
      [Required]
      [Column("CCompanyCode")]
      [StringLength(3)]
      public string CcompanyCode { get; set; }
      [Required]
      [Column("CCountryCode")]
      [StringLength(3)]
      public string CcountryCode { get; set; }
      [Required]
      [Column("CPhone")]
      [StringLength(7)]
      public string Cphone { get; set; }
      [Required]
      public string Address { get; set; }
      [Column("GLat", TypeName = "decimal(10, 8)")]
      public decimal? Glat { get; set; }
      [Column("GLng", TypeName = "decimal(11, 8)")]
      public decimal? Glng { get; set; }
      public bool BuyerFlag { get; set; }
      public bool SellerFlag { get; set; }
      public short CityId { get; set; }
  
      [ForeignKey("CityId")]
      [InverseProperty("Users")]
      public virtual City City { get; set; }
      [InverseProperty("Seller")]
      public virtual ICollection&lt;Advertisement&gt; Advertisements { get; set; }
      [InverseProperty("Buyer")]
      public virtual ICollection&lt;BuyerAddsDifferentAdsToFav&gt; BuyerAddsDifferentAdsToFavs { get; set; }
      [InverseProperty("Buyer")]
      public virtual ICollection&lt;BuyersAddAgroItemToInterest&gt; BuyersAddAgroItemToInterests { get; set; }
      [InverseProperty("Buyer")]
      public virtual ICollection&lt;SellersFavoritesBuyer&gt; SellersFavoritesBuyerBuyers { get; set; }
      [InverseProperty("Seller")]
      public virtual ICollection&lt;SellersFavoritesBuyer&gt; SellersFavoritesBuyerSellers { get; set; }
  }
  </code></pre>
  
  <p>I am using this method of repository to get the user</p>
  
  <pre><code>public EFarmer.Models.User GetUser(ContactNumberFormat contact)
  {
      var user = users
          .Where(x =&gt; x.CcountryCode == contact.CountryCode
          &amp;&amp; x.CcompanyCode == contact.CompanyCode
          &amp;&amp; x.Cphone == contact.PhoneNumber).First() ?? null;
      return (user != null) ? new EFarmer.Models.User
      {
          Address = user.Address,
          City = EFarmer.Models.City.Convert(user.City),
          ContactNumber = new ContactNumberFormat(user.CcountryCode, user.CcompanyCode, user.Cphone),
          IsBuyer = user.BuyerFlag,
          IsSeller = user.SellerFlag,
          Location = new GeoLocation { Latitude = user.Glat, Longitude = user.Glng },
          Name = new NameFormat { FirstName = user.Fname, LastName = user.Lname },
          Id = user.Id
      } : null;
  }
  </code></pre>
  
  <p>And here is the convert method which converts Entity model to my Business Model, it give null reference exception due to null in City from entity model</p>
  
  <pre><code>public static City Convert(EFarmerPkModelLibrary.Entities.City city)
  {
      return new City
      {
          GeoLocation = new GeoLocation { Latitude = city.Glat, Longitude = city.Glng },
          Id = city.Id,
          Name = city.Name
      };
  }
  </code></pre>