Skip to content

Compiled models with JSON columns #29602

Closed
@ramonesz297

Description

@ramonesz297

Does the Compiled model support JSON columns?

I did not find any info about this limitation, but when I try to compile a model for context that has configured JSON columns -

System.InvalidOperationException: Cannot scaffold C# literals of type 'Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerJsonTypeMapping'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpRuntimeAnnotationCodeGenerator.GenerateSimpleAnnotations(CSharpRuntimeAnnotationCodeGeneratorParameters parameters)        
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpRuntimeAnnotationCodeGenerator.Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters) 
   at Microsoft.EntityFrameworkCore.Design.Internal.RelationalCSharpRuntimeAnnotationCodeGenerator.Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.SqlServer.Design.Internal.SqlServerCSharpRuntimeAnnotationCodeGenerator.Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.CreateAnnotations[TAnnotatable](TAnnotatable annotatable, Action`2 process, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.CreateAnnotations(IEntityType entityType, IndentedStringBuilder mainBuilder, IndentedStringBuilder methodBuilder, SortedSet`1 namespaces, String className, Boolean nullable)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.GenerateEntityType(IEntityType entityType, String namespace, String className, Boolean nullable)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator.GenerateModel(IModel model, CompiledModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.CompiledModelScaffolder.ScaffoldModel(IModel model, String outputDir, CompiledModelCodeGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.Optimize(String outputDir, String modelNamespace, String contextTypeName)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContextImpl(String outputDir, String modelNamespace, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OptimizeContext.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Cannot scaffold C# literals of type 'Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerJsonTypeMapping'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.
using Microsoft.EntityFrameworkCore;

public class Info
{
    public string? Code { get; set; }
}

public class Address
{
    public string? Name { get; set; }

    public Info? Info { get; set; }
}

public class Package
{
    public int Id { get; set; }

    public string? Name { get; set; }


    public Address[]? Addresses { get; set; }
}

public class AppContext : DbContext
{

    public DbSet<Package> Packages => Set<Package>();


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=test123;");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Package>(o =>
        {
            o.HasKey(x => x.Id);
            o.OwnsMany(w => w.Addresses, b =>
            {
                b.ToJson();
                b.OwnsOne(w => w.Info);
            });
        });

        base.OnModelCreating(modelBuilder);
    }
}

EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 7.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.4)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions