Skip to content

Instantly share code, notes, and snippets.

@shahabganji
Last active March 13, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahabganji/7bdd7fe3adf40365c4567bbd4139db5e to your computer and use it in GitHub Desktop.
Save shahabganji/7bdd7fe3adf40365c4567bbd4139db5e to your computer and use it in GitHub Desktop.
EfCore-Array-Of-String-Sample
public sealed class Book
{
private List<string> _tags;
private Book()
{
_tags = new List<string>();
}
public Book(string name)
{
Id = Guid.NewGuid();
Name = name;
_tags = new List<string>();
}
public Guid Id { get; }
public string Name { get; }
public IReadOnlyCollection<string> Tags => _tags.AsReadOnly();
public void AddTag(params string[] tags) => _tags = new List<string>(_tags.Union(tags));
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sample.EFCore.ArrayPrimitiveTypes.Domain;
using Sample.EFCore.ArrayPrimitiveTypes.Storage.Converters;
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)
.HasConversion<TagsValueConverter>();
}
}
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)
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Sample.EFCore.ArrayPrimitiveTypes.Storage.Converters;
public class TagsValueConverter : ValueConverter<IReadOnlyCollection<string>, string[]>
{
public TagsValueConverter() : base(
value => value.ToArray(),
dbValue => dbValue.ToList())
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment