以下是具有复杂属性的类型:
[Table("contact")]
public class Contact
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("details", TypeName = "jsonb")]
public Details Details { get; set; }
...
}
[ComplexType]
public class Details
{
[JsonProperty("firstName")]
public string FirstName { get; set; }
[JsonProperty("middleName")]
public string MiddleName { get; set; }
[JsonProperty("lastName")]
public string LastName { get; set; }
}
对于pgsql提供程序来说,这很好用:
services.AddDbContextPool<ProfileDbContext>((sp, options) =>
{
options.UseNpgsql(connStr,
x =>
{
x.EnableRetryOnFailure();
x.CommandTimeout(120);
});
});
但是在将它替换为内存提供程序时,在 TestsStartup 中
services.AddDbContextPool<ProfileDbContext>(options =>
{
ServiceProvider sp = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
options.UseInMemoryDatabase("Profile", _databaseRoot);
options.UseInternalServiceProvider(sp);
});
出现错误:
实体类型“Details”需要定义主键。如果您打算使用无键实体类型,请调用“HasNoKey()”。
如果我通过Fluent配置指定关联OwnsOne,则在InMemory中可以正常工作,但在使用pgsql提供程序启动时失败。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Contact>(c => c.OwnsOne(e => e.Details));
}
我猜这是因为jsonb字段类型不被内存提供程序支持,是吗?
那么集成测试该怎么办呢?
我们在所有测试中都使用了内存提供程序,但这个jsonb功能似乎破坏了与内存提供程序的技巧。