sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
700 stars 230 forks source link

Filter on Table-Index ignored #803

Open JStarkl opened 1 year ago

JStarkl commented 1 year ago

I have indices with filter values, but those won't be generated (SQLServer)

An index looks like this:

UNIQUE NONCLUSTERED INDEX [IX_person] ON [dat].[person] ([person_id] ASC)
WHERE ([primary_flag] = (1) AND [inactive_flag] = (0))

Will generate:

builder.HasIndex(x => new { x.PersonId })
    .HasDatabaseName("IX_person")
    .IsUnique()

But the expected result is:

builder.HasIndex(x => new { x.PersonId })
    .HasFilter("([primary_flag]=(1) AND [inactive_flag]=(0))")
    .HasDatabaseName("IX_person")
    .IsUnique()

I checked EF.Reverse.POCO.v3.ttinclude and it seems this is not implemented.

I currently use version 3.8.0 with a EFCore7 Model

Best regards, Jürgen

JStarkl commented 1 year ago

I did a quick hack for my current purposes, and the "HasFilter"-Attribute perfectly does it's Job

EF.Revers.POCO.v3.ttinclude in the method [IndexModelBuilder]:

From:

if (indexesForName.Count > 1)
    sb.Append(" }");

sb.Append(")"); // Close bracket for HasIndex()

sb.Append(isEfCore5Plus ? ".HasDatabaseName(\"" : ".HasName(\"");
sb.Append(indexName);
sb.Append("\")");

To:

if (indexesForName.Count > 1)
    sb.Append(" }");

sb.Append(")"); // Close bracket for HasIndex()

//>>>
if (indexName == "IX_person_email_person_id_primary" | indexName == "IX_person_phone_person_id_primary")
{
    sb.Append(".HasFilter(\"([primary_flag]=(1) AND [inactive_flag]=(0))\")");
}
//<<<

sb.Append(isEfCore5Plus ? ".HasDatabaseName(\"" : ".HasName(\"");
sb.Append(indexName);
sb.Append("\")");
sjh37 commented 1 year ago

Your if statement should change from a single | to a double ||

if (indexName == "IX_person_email_person_id_primary" || indexName == "IX_person_phone_person_id_primary")