表每层级表缺少子类型的列



我有一个问题,为超级类型VeganItem创建的数据库表没有RecipeItem子类型列:servesCount,cookTime,prepTime,method,tips,ingredients在EF Core中使用表每层次结构时。

超类:

[Serializable]
public abstract class VeganItem : DomainEntity<int>
{
[Required]
public string Name { get; set; }
[Required]
public string CompanyName { get; set; }
public string Description { get; set; }
[Required]
public int IsNotVeganCount { get; set; } = 0;
[Required]
public int IsVeganCount { get; set; } = 0;
[Required]
public int RatingsCount { get; set; } = 0;
[Required]
public int Rating { get; set; }
public List<Option> Tags { get; set; }
[MaxLength(10)]
public List<String> Images { get; set; }
}
public abstract class VeganItem<TVeganItemEstablishment> : VeganItem 
where TVeganItemEstablishment : VeganItemEstablishment
{
[PropertyName("veganItemEstablishments", Ignore = true)]
public virtual ICollection<TVeganItemEstablishment> VeganItemEstablishments { get; set; }
}

子类型:

[ElasticsearchType(RelationName = "recipeitem", IdProperty = "Id")]
public class RecipeItem : VeganItem<RecipeItemEstablishment>
{
[Required]
public int servesCount;
[Required]
public TimeSpan cookTime;
[Required]
public TimeSpan prepTime;
[Required]
public Option difficulty;
[Required]
[MaxLength(100)]
public List<string> method;
[Required]
[MaxLength(2)]
public List<string> tips;
[Required]
[MaxLength(100)]
public List<string> ingredients;
}

模型构建器:

modelBuilder.Entity<RecipeItem>(veganItem =>
{
veganItem.Property(e => e.difficulty)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<Option>(v, null)
);
});
modelBuilder.Entity<VeganItem>(veganItem =>
{
veganItem.HasIndex("CompanyName", "Name", "Discriminator").IsUnique();
veganItem.HasDiscriminator<string>("Discriminator");
veganItem.Property(u => u.CreatedDate)
.HasDefaultValueSql("CURRENT_TIMESTAMP"); 
veganItem.Property(u => u.UpdatedDate)
.HasDefaultValueSql("CURRENT_TIMESTAMP"); 
veganItem.HasKey(e => e.Id);
veganItem.HasOne(q => q.UpdatedBy)
.WithMany()
.HasForeignKey(k => k.UpdatedById);
veganItem.HasOne(q => q.CreatedBy)
.WithMany()
.HasForeignKey(k => k.CreatedById);
veganItem.Property(e => e.Tags)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<List<Option>>(v, null)
);
});
public DbSet<VeganItem> VeganItems { get; set; }     
public DbSet<RecipeItem> RecipeItems { get; set; }

difficulty创建一个列。我想是因为这个属性在数据上下文中有一个值转换器。

任何想法如何得到在问题的顶部提到的字段出现在数据库表中?

编辑:这使它工作-我只是不认为这是必要的

modelBuilder.Entity<RecipeItem>(veganItem =>
{
veganItem.Property(e => e.Difficulty)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<Option>(v, null)
);
veganItem.Property(e => e.Tips);
veganItem.Property(e => e.ServesCount);
veganItem.Property(e => e.CookTime);
veganItem.Property(e => e.PrepTime);
veganItem.Property(e => e.Method);
veganItem.Property(e => e.Ingredients);
});

实体框架不支持基本类型集合。你可以创建一个实体(它将被保存到另一个表中),也可以做一些字符串处理来将列表保存为字符串,并在实体具体化后填充列表。

  • 您已经将所有列创建为字段。没有属性。(使它们成为属性)即

    public TypeName propName {get;set;}

,然后运行迁移或命令。

相关内容

  • 没有找到相关文章