Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 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 Reply

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...