C#C
C#3y ago
Sebastian

✅ EF Json column - object reference not set to an instance of an object

Hey, I am trying to get JSON columns work with EF.
Microsoft.EntityFrameworkCore - 7.0.2
Microsoft.EntityFrameworkCore.Design - 7.0.2
Microsoft.EntityFrameworkCore.Tools - 7.0.2
Pomelo.EntityFrameworkCore.MySql - 7.0.0 (Probably not relevant as I fail on first migration)


When creating my initial migration I am met with:
Object reference not set to an instance of an object

public class AppDbContext : DbContext
{
  internal DbSet<Store> Stores => Set<Store>();

  public AppDbContext(DbContextOptions options)
    : base(options)
  { }

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
    modelBuilder
      .Entity<Store>()
      .OwnsOne(store => store.Address, builder => builder.ToJson());
  }
}

public class Store
{
    [Key]
    public int Id { get; set; }

    /* Some other properties */

    // When line below is commented migration is generated without fail
    public AddressData Address { get; set; }
}

public class AddressData
{
    public string Country { get; set; }
    public string Region { get; set; }
    public string Postcode { get; set; }
    public string District { get; set; }
    public string Place { get; set; }
    public string Locality { get; set; }
    public string Neighborhood { get; set; }
    public string Address { get; set; }
    public string Poi { get; set; }
}


Migration only fails when I include Adress part of Store.
Any ideas what could be the issue?
Was this page helpful?