属性'Country.IdCountry'属于当前数据库提供程序不支持的 'Guid' 类型



我正在尝试将EF Core与Azure CosmosDB结合使用。我使用以下配置:

实体:

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

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());
}

和IServiceCollection配置:

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

但是我仍然得到以下错误:

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'

编辑:我忘了说明。当我尝试向上下文集合

添加新项时,就会发生这种情况。我是CosmosDB的新手,也是EF配置的新手。

你知道问题出在哪里吗?

好了,我明白了,我用错了ValueConverter。我使用了

.HasConversion<GuidToStringConversion>()

但是我不得不使用

.HasConversion<string>()

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

我希望它会有人:)

最新更新