Skip to content

Compiled models with JSON columns #29602

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ramonesz297 opened this issue Nov 17, 2022 · 3 comments · Fixed by #30254
Closed

Compiled models with JSON columns #29602

ramonesz297 opened this issue Nov 17, 2022 · 3 comments · Fixed by #30254

Comments

@ramonesz297
Copy link

ramonesz297 commented Nov 17, 2022

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)

@ajcvickers
Copy link
Contributor

Note from triage: the annotation needs to be filtered out.

@bricelam bricelam removed their assignment Jan 5, 2023
@bricelam
Copy link
Contributor

bricelam commented Jan 5, 2023

@maumar They may just need to be removed in SqlServerCSharpRuntimeAnnotationCodeGenerator, but I'm not sure what implications that has at runtime and whether we'll need to add code elsewhere to compensate.

@ajcvickers ajcvickers assigned ajcvickers and unassigned maumar Feb 10, 2023
@ajcvickers
Copy link
Contributor

Poaching.

ajcvickers added a commit that referenced this issue Feb 11, 2023
Fixes #29602

The main change here is to store the JSON type mapping in the relational model, and obsolete the previous storage in the underlying EF model.
ajcvickers added a commit that referenced this issue Feb 18, 2023
Fixes #29602

The main change here is to store the JSON type mapping in the relational model, and obsolete the previous storage in the underlying EF model.
ajcvickers added a commit that referenced this issue Mar 1, 2023
Fixes #29602

The main change here is to store the JSON type mapping in the relational model, and obsolete the previous storage in the underlying EF model.
@ajcvickers ajcvickers modified the milestones: 8.0.0, 8.0.0-preview3 Mar 13, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0-preview3, 8.0.0 Nov 14, 2023
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants