gpt4 book ai didi

azure - 属性 'Country.IdCountry' 的类型为 'Guid',当前数据库提供程序不支持该类型

转载 作者:行者123 更新时间:2023-12-03 05:30:29 32 4
gpt4 key购买 nike

我正在尝试将 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; }
}

实体配置类:

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")
)
);

但我仍然收到以下错误:

属性“Country.IdCountry”的类型为“Guid”,当前数据库提供程序不支持该类型。更改属性 CLR 类型,或者使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略该属性

编辑:我忘了指定。当我尝试将新项目添加到上下文集合时会发生这种情况

我是 CosmosDB 的新手,也是通过这种方式配置 EF 的。

您知道问题出在哪里吗?

最佳答案

好吧,我明白了,我用错了 ValueConverter。我用过

.HasConversion<GuidToStringConversion>()

但我必须使用

.HasConversion<string>()

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

我希望有人:)

关于azure - 属性 'Country.IdCountry' 的类型为 'Guid',当前数据库提供程序不支持该类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65926942/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com