Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

azure - The property 'Country.IdCountry' is of type 'Guid' which is not supported by the current database provider

I'm trying to use EF Core in combination with Azure CosmosDB. I'm using the following configuration:

Entities:

public class Country
{
    public Guid IdCountry { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public string PhoneCode { get; set; }
    public IdNameReference Currency { get; set; }
}

public class IdNameReference
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

EntityConfiguration class:

public class CountryEntityConfiguration : IEntityTypeConfiguration<Country>
{
    public void Configure(EntityTypeBuilder<Country> builder)
    {
        builder.ToContainer("Country");
        builder.HasKey(e => e.IdCountry);
        builder.HasPartitionKey(e => e.IdCountry);
        builder.HasNoDiscriminator();
        builder.Property(e => e.IdCountry).HasConversion<GuidToStringConverter>().ValueGeneratedOnAdd().HasValueGenerator<GuidValueGenerator>();
        builder.HasOne(e => e.Currency).WithMany().Metadata.DependentToPrincipal.SetPropertyAccessMode(PropertyAccessMode.Field);
    }
}

DbContext OnModelCreating:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    builder.ApplyConfiguration(new CurrencyEntityConfiguration());
}

And the IServiceCollection configuration:

services.AddDbContext<MyDbContext>(options => options.UseCosmos(
    Environment.GetEnvironmentVariable("Db_ServiceEndpoint"),
    Environment.GetEnvironmentVariable("Db_AccountKey"),
    Environment.GetEnvironmentVariable("Db_DatabaseName")
    )
);

But I'm still getting the following error:

The property 'Country.IdCountry' is of type 'Guid' 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'

EDIT: I forgot to specify. This happens when I try to add a new item to the context collection

I'm new in CosmosDB and also in configuration of EF this way.

Do you have an idea, where the problem could be?

question from:https://stackoverflow.com/questions/65926942/the-property-country-idcountry-is-of-type-guid-which-is-not-supported-by-the

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay, I figured it out, I used wrong the ValueConverter. I used

.HasConversion<GuidToStringConversion>()

but I had to use

.HasConversion<string>()

or

.HasConversion(g => g.ToString("D"), s => new Guid(s))

I hope it will someone :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...