Skip to content

IReadOnlyCollection<string> throws an exception with CosmosDB #27643

@shahabganji

Description

@shahabganji

Related Issue: #25344

I am declaring a property of type IReadOnlyCollection<string> and wanted to store it as a string array in the CosmosDB, I am getting the following error:

Unhandled exception. System.InvalidOperationException: The property 'Book.Tags' is of type 'IReadOnlyCollection<string>' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidatePropertyMapping(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal.CosmosModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)

In the configuration file for the Book entity, I used the following:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sample.EFCore.ArrayPrimitiveTypes.Domain;


namespace Sample.EFCore.ArrayPrimitiveTypes.Storage.Configurations;
public sealed class BookConfiguration : IEntityTypeConfiguration<Book>
{
    public void Configure(EntityTypeBuilder<Book> builder)
    {
        builder.ToContainer("Books");

        builder.HasKey(p => p.Id);
        builder.HasPartitionKey(p => p.Id);
        builder.Property(p => p.Id)
            .ToJsonProperty("id")
            .IsRequired();

        builder.Property(p => p.Name).IsRequired();

        builder.Property(p => p.Tags);
    }
}

Even when I change the last line to

builder.Property(p => p.Tags).HasField("_tags").UsePropertyAccessMode(PropertyAccessMode.Field)

The error is gone if I either use a List<string> with no backing field or use a conversion for converting IReadOnlyCollection<string> to string[].


Provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.Cosmos
Target framework: net6.0
Operating system: mac OS Monterey
IDE: RIder 2022


PS: You could find an application that reproduce the error in this repository/branch. When cloning the application, make sure the reproduce/efcore-error branch is checked out. The Main branch already has the conversion included.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions