title: nullreferenceexception: object reference not set to an instance of an object in a database call
body: <p>I'm getting a nullreferenceexception in a method of my repository class, when I'm trying to retrieve all records of a model, I've read about this exception and I think I understand it but I don't get why it is happening in my code.</p>
  
  <p>I have this model: </p>
  
  <pre><code>public class Post
  {
  
      [Key]
      public int Id { get; set; }
      public string Title { get; set; } = "";
      public string Body { get; set; } = "";
      public DateTime? Created { get; set; } = DateTime.Now;
  }
  </code></pre>
  
  <p>this is my context: </p>
  
  <pre><code>public class AppDbContext : DbContext
  {
      public AppDbContext(DbContextOptions options) : base(options) { }
      public DbSet&lt;Post&gt; Posts { get; set; }
  }
  </code></pre>
  
  <p>The method "GetAllPost()" in my repository class: </p>
  
  <pre><code>    public async Task&lt;List&lt;Post&gt;&gt; GetAllPost()
      {
          var listOfPost = _context.Posts;//here is where the exception is thrown
          return await listOfPost.ToListAsync();
      }
  </code></pre>
  
  <p>This is my entire Repository Class: </p>
  
  <pre><code>public class Repository : IRepository
  {
      private readonly AppDbContext _context;
  
      public Repository(AppDbContext context) {
          _context = context;
      }
  
      public async Task AddPost(Post post)
      {
          await _context.Posts.AddAsync(post);
          await _context.SaveChangesAsync();
      }
  
      public async Task&lt;List&lt;Post&gt;&gt; GetAllPost()
      {
          var listOfPost = _context.Posts;
  
          return await listOfPost.ToListAsync();
      }
  
      public async Task&lt;Post&gt; GetPost(int id)
      {
          return await _context.Posts.FirstOrDefaultAsync(x=&gt; x.Id == id);
      }
  
      public async Task RemovePost(Post post)
      {
           _context.Remove(post);
           await _context.SaveChangesAsync();
      }
  
      public async Task UpdatePost(Post post)
      {
           _context.Posts.Update(post);
          await _context.SaveChangesAsync();
      }
  }
  </code></pre>
  
  <p>and this is my ConfigureService Method(I'm using postgres by the way): </p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
              services.AddControllersWithViews();
  
             services.AddEntityFrameworkNpgsql().AddDbContext&lt;AppDbContext&gt;(
                 x =&gt; x.UseNpgsql(
                     Configuration.GetConnectionString("DefaultConnection")
                     )
                 );
  
              services.AddTransient&lt;IRepository, Repository&gt;();
  
          }
  </code></pre>
  
  <p>I have my database created with migrations and populated </p>