In EF Core, is it possible to index properties within a complex type which is within another complex type which I plan on querying and where the indexes will ultimately matter?
If I have an object which I want to optimize its queries, and a property within it is a complex type, can I index the properties within this complex type in reference to the outer object's queries?
Here is an example of the two objects, the one as a property within the other:
public class AuditableEntity
{
public int Id { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
[Required]
public string CreatedBy { get; set; }
}
and
public class NotificationItem : IAuditableEntityComposition
{
public AuditableEntity AuditableEntity { get; set; }
}
So, for instance I'd like to query on
NotificationItem.AuditableEntity.CreatedOn
I'd like to have an optimized query for AuditableEntity.CreatedOn
when there is a collection of some sort that is referencing this, such as a list of NotificationItem
that needs to query on CreatedOn
.
I'm trying to use fluent syntax. For instance:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<NotificationItem>()
.HasIndex(ni => ni.AuditableEntity.CreatedOn);
}
The nested ni.AuditableEntity.CreatedOn
doesn't work.
If you have any questions, need more information, or I did something wrong, please let me know and I'll do my best to fix it.
question from:
https://stackoverflow.com/questions/66057810/ef-core-is-it-possible-to-index-properties-within-a-complex-type-which-is-withi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…