C
C#2y ago
Natro

✅ 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)
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; }
}
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?
8 Replies
Angius
Angius2y ago
MySQL doesn't support JSON columns to begin with Unless EF encodes the data into a string I can't see it working
jcotton42
jcotton422y ago
but it does https://dev.mysql.com/doc/refman/5.7/en/json.html ? doesn't mean the EF driver does tho
Angius
Angius2y ago
Huh, must be a new addition I stand corrected
jcotton42
jcotton422y ago
it's still memesql tho
Natro
NatroOP2y ago
JSON Columns in Entity Framework Core
EF Core 7.0 contains provider-agnostic support for JSON columns, with an implementation for SQL Server. This support allows the mapping of aggregates built from .NET types to JSON documents.
Natro
NatroOP2y ago
From my understanding the thing I am doing is possible?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server