属性 'Country.IdCountry' 的类型 'Guid' 当前数据库提供程序不支持
The property 'Country.IdCountry' is of type 'Guid' which is not supported by the current database provider
我正在尝试将 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; }
}
实体配置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());
}
以及 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))
我希望有人会:)
我正在尝试将 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; }
}
实体配置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());
}
以及 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))
我希望有人会:)