Description
Ask a question
I have decided to use table splitting to split my Product model into two classes - Product and ProductExtraDetail, because the latter contains a long description field that I don't usually want to return in queries. However, I would like to add a generated NpgsqlTsVector column for a full text search on the product. This contains fields from both the main Product itself, such as display name, and fields from the ProductExtraDetail such as description. I don't really want to repeat the fields from Product in the definition of ProductExtraDetail. I'm wondering what the best way to achieve this is. Is it allowed to reference a navigation property in the fields for the HasGeneratedTsVectorColumn function? For example can I do something like this?
namespace PictureflectLicenseServer.DbModels {
[Table("Products")]
public class Product {
[Key]
[Column("Id")]
public Guid Id { get; set; }
[Required]
[Column("DisplayName")]
public string DisplayName { get; set; }
public ProductExtraDetail ExtraDetail { get; set; }
}
[Table("Products")]
public class ProductExtraDetail {
[Key]
[Column("Id")]
public Guid Id { get; set; }
[Column("Description")]
public string Description { get; set; }
[Column("FullSearchVector")]
public NpgsqlTsVector FullSearchVector { get; set; } //A generated column for text search
public Product Parent { get; set; }
}
public class ProductEntityTypeConfiguration : IEntityTypeConfiguration<Product> {
public void Configure(EntityTypeBuilder<Product> builder) {
builder.HasOne(item => item.ExtraDetail).WithOne(item => item.Parent).HasForeignKey<ProductExtraDetail>(item => item.Id);
}
}
public class ProductExtraDetailEntityTypeConfiguration : IEntityTypeConfiguration<ProductExtraDetail> {
public void Configure(EntityTypeBuilder<ProductExtraDetail> builder) {
builder.HasGeneratedTsVectorColumn(item => item.FullSearchVector, "english", item => new { item.Parent.DisplayName, item.Description }).HasIndex(item => item.FullSearchVector).HasMethod("GIN");
}
}
}Note the reference to Parent.DisplayName in the last line - is that valid?
If not, can I use shadow properties somehow? Or do I just have to repeat the DisplayName property in ProductExtraDetail? Also, in the case of repeating the property, I'm not quite sure how that works when updating. Do I have to update the property in both places?
Activity
roji commentedon Oct 21, 2021
@benstevens48 at the end of the day, your columns do co-inhabit the same table, so there's no problem with the generated column referencing the other column, even if in the EF Core model they're on different entity types. I'm not quite sure whether this would work with HasGeneratedTsVectorColumn - what are you seeing with the code above? But even if there's an issue, note that HasGeneratedVectorColumn is just sugar over specifying the computed column SQL; you can always just do that your self with HasComputedColumnSql.
benstevens48 commentedon Oct 21, 2021
@roji - actually, no the above code doesn't work. I get the error
Instead, I am following your suggestion and doing
Does that look right?
Is there any chance that the code which references the Parent navigation property, i.e.
could be supported at some point? I like to avoid manually writing SQL and hard-coding the column names if possible. I imagine that this could be quite a common scenario for table splitting. Or perhaps just add a variant of the function that accepts the column names as strings instead of a LINQ expression? (PS part of the reason I'm using table splitting is due to the lack of a good solution to #1387, so enabling that would also solve the problem for me).
roji commentedon Oct 21, 2021
It's possible, but I doubt I'll have time any time soon to work on it, given that nobody's complained so far and there's a solid workaround with HasComputedColumnSql... But you can definitely open an issue on https://github.com/npgsql/efcore.pg.
At that point there really is no value over just using HasComputedColumnSql, no? You'd be hard-coding the names either way?
If partial loading is why you're using table splitting, then keep in mind that you can always project out whatever you want with Select, including the original entity type:
While this allows you to partially load blogs, it doesn't track them so cannot be used for later automatically-tracked updates.
benstevens48 commentedon Oct 22, 2021
@roji - before I forget, I think I encountered a migration bug. I made the above change to the
FullSearchVectorcolumn. Then I generated a migration, which created code to drop and re-add theFullSearchVectorcolumn. However, it did not re-create the index on this column. Neither did it drop the index, but obviously the index was dropped automatically when the column was dropped. Should I report this as a migrations bug? I guess that the migration code should probably drop the index before dropping the column then re-add it after re-adding the column.Will do at some point.
Yeah I guess so. I wrote my own helper function for this. It's just if I have a helper function that I know I'm going to need in every project, I can't help thinking that either it should be built-in or I'm doing something wrong!
Yeah, I know this, but my
Productmodel above is nested 1-3 levels deep in several other model classes. It's possible to reuse partial selection code reasonably easily for top level items, but for navigation properties it's pretty difficult/impossible I think. Having to write out all the properties each time became unmanageable, hence the move to table-splitting, which seems to provide a better, if not 100% satisfactory, solution.roji commentedon Oct 22, 2021
Yeah, can you please open an issue for that with the full details and repro steps?
skyZcoding commentedon Aug 24, 2022
@benstevens48 Hi Ben
I'm running into the same issue.
How did you got it fix with your helper function? Could you share it with me?
benstevens48 commentedon Aug 24, 2022
@skyZcoding I have a very simple helper function like this
and I use it like this
(instead of something like this without table splitting)