C
C#15mo 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
Angius15mo 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
jcotton4215mo ago
but it does https://dev.mysql.com/doc/refman/5.7/en/json.html ? doesn't mean the EF driver does tho
Angius
Angius15mo ago
Huh, must be a new addition I stand corrected
jcotton42
jcotton4215mo ago
it's still memesql tho
Natro
Natro15mo 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
Natro15mo ago
From my understanding the thing I am doing is possible?
Accord
Accord15mo 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
More Posts