EF can you make a mutli-column index using shadow properties?

I am trying to create a unique index with multiple columns using the shadow property. I know that I can solve this problem by simply adding a property, but I would like to see if this is possible if not to make it so that my model is clean.

To create a multi-column index, you have the following parameter in the Fluent API:

modelBuilder.Entity<AlbumTrack>().HasIndex(t => new { t.TrackNumber, t.AlbumId).IsUnique();

But I don’t want to clutter up my model with the additional AlbumId property and, therefore, I want to use the shadow property, for one column this works:

modelBuilder.Entity<AlbumTrack>().HasIndex(t => EF.Property<int>(t,"AlbumId")).IsUnique();

But for an index with multiple columns using the shadow property, like the following

modelBuilder.Entity<AlbumTrack>()
    .HasIndex(t => new { t.TrackNumber, EF.Property<int>(t,"AlbumId")})
    .IsUnique();

I get the following error in vscode

. , .

- , ?

+4
1

. HasIndex params string[] propertyNames.

, shadow :

modelBuilder.Entity<AlbumTrack>()
    .Property<int>("AlbumId");

:

modelBuilder.Entity<AlbumTrack>()
    .HasIndex("TrackNumber", "AlbumId")
    .IsUnique();
+4

Source: https://habr.com/ru/post/1690636/


All Articles