My code (.NET 8.0 preview 6) to allow ToJson()
in SQLite:
using Microsoft.EntityFrameworkCore;
var ctx = new TestContext();
ctx.Database.EnsureCreated();
ctx.Items.ToArray();
class TestContext : DbContext
{
public DbSet<TestItem> Items { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=test.db");
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TestItem>().OwnsMany(ti => ti.Children, builder => builder.ToJson());
}
}
class TestItem
{
public required int Id { get; set; }
public required string Name { get; set; }
public required List<TestChild> Children { get; set; }
}
class TestChild
{
public required int Id { get; set; }
public required string Name { get; set; }
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0-preview.6.23329.4" />
</ItemGroup>
</Project>
I get the following error when loading the (empty) items table:
Exception thrown: 'System.ArgumentException' in System.Linq.Expressions.dll An unhandled exception of type 'System.ArgumentException' occurred in System.Linq.Expressions.dll Expression of type 'System.Object' cannot be used for assignment to type 'System.Int32'
It seem related to the children list. I can do a similar model with one child (instead of children) that works. The database is empty at this point so it is not about loaded data content.