如何添加 derived class 属性 EF Core
How add derived class property EF Core
我正在进行迁移,但没有看到继承者 class 的 属性。
我有下一个模型。在 class SomeClass 中,我有正确显示的虚拟 属性,但是在 class 中继承时,没有显示
public class School
{
[Key]
public long Id { get; init; }
[Column(TypeName = "jsonb")]
public SomeClass Parameter1 { get; set; }
}
public class SomeClass
{
public virtual ParameterTypes Type { get; }
}
public enum ParameterTypes
{
None,
Value
}
public class DerivedClass : SomeClass
{
public override ParameterTypes Type => ParameterTypes.Value;
public string Value { get; set; }
}
我正在添加配置此模型
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<School>(
builder =>
{
builder.HasKey(h => h.Id);
builder.Property(h => h.Parameter1).HasJsonConversion();
});
}
public static PropertyBuilder<T> HasJsonConversion<T>(this PropertyBuilder<T> propertyBuilder)
where T : class, new()
{
var converter = new ValueConverter<T, string>(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<T>(v, null) ?? new T());
var comparer = new ValueComparer<T>(
(l, r) => JsonSerializer.Serialize(l, null) == JsonSerializer.Serialize(r, null),
v => v == null ? 0 : JsonSerializer.Serialize(v, null).GetHashCode(),
v => JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(v, null), null)
);
propertyBuilder.HasConversion(converter);
propertyBuilder.Metadata.SetValueConverter(converter);
propertyBuilder.Metadata.SetValueComparer(comparer);
propertyBuilder.HasColumnType("jsonb");
return propertyBuilder;
}
当我进行迁移时,我将枚举视为一个字符串(右),但我没有看到 属性 DerivedClass.Value
也许问题出在 HasJsonConversion 方法中?
如何从继承的 class 中得到 属性?
var converter = new ValueConverter<T, string>(
v => JsonSerializer.Serialize(v, **v.GetType()**, jsonSerializerOptions),
v => JsonSerializer.Deserialize<T>(v, jsonSerializerOptions));
我正在进行迁移,但没有看到继承者 class 的 属性。 我有下一个模型。在 class SomeClass 中,我有正确显示的虚拟 属性,但是在 class 中继承时,没有显示
public class School
{
[Key]
public long Id { get; init; }
[Column(TypeName = "jsonb")]
public SomeClass Parameter1 { get; set; }
}
public class SomeClass
{
public virtual ParameterTypes Type { get; }
}
public enum ParameterTypes
{
None,
Value
}
public class DerivedClass : SomeClass
{
public override ParameterTypes Type => ParameterTypes.Value;
public string Value { get; set; }
}
我正在添加配置此模型
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<School>(
builder =>
{
builder.HasKey(h => h.Id);
builder.Property(h => h.Parameter1).HasJsonConversion();
});
}
public static PropertyBuilder<T> HasJsonConversion<T>(this PropertyBuilder<T> propertyBuilder)
where T : class, new()
{
var converter = new ValueConverter<T, string>(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<T>(v, null) ?? new T());
var comparer = new ValueComparer<T>(
(l, r) => JsonSerializer.Serialize(l, null) == JsonSerializer.Serialize(r, null),
v => v == null ? 0 : JsonSerializer.Serialize(v, null).GetHashCode(),
v => JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(v, null), null)
);
propertyBuilder.HasConversion(converter);
propertyBuilder.Metadata.SetValueConverter(converter);
propertyBuilder.Metadata.SetValueComparer(comparer);
propertyBuilder.HasColumnType("jsonb");
return propertyBuilder;
}
当我进行迁移时,我将枚举视为一个字符串(右),但我没有看到 属性 DerivedClass.Value
也许问题出在 HasJsonConversion 方法中?
如何从继承的 class 中得到 属性?
var converter = new ValueConverter<T, string>(
v => JsonSerializer.Serialize(v, **v.GetType()**, jsonSerializerOptions),
v => JsonSerializer.Deserialize<T>(v, jsonSerializerOptions));