Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

IReadOnlyCollection<string> throws an exception with CosmosDB #27643

Closed
shahabganji opened this issue Mar 14, 2022 · 2 comments
Closed

IReadOnlyCollection<string> throws an exception with CosmosDB #27643

shahabganji opened this issue Mar 14, 2022 · 2 comments

Comments

@shahabganji
Copy link

shahabganji commented Mar 14, 2022

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.

@ajcvickers
Copy link
Member

@shahabganji IReadOnlyCollection is not supported because it's too generic for use to map appropriately. Use IReadOnlyList or IReadOnlyDictionary instead.

@shahabganji
Copy link
Author

shahabganji commented Mar 16, 2022

That makes sense @ajcvickers 👍🏼; it is not also working when I use backing field of List<string>, is it also something that cannot be considered?

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants