Search code examples
.netentity-framework-coreentity-framework-migrationsscaffolding

dotnet ef dbcontext scaffold command --data-annotations or -d command line parameter doesn't seem to work


I have a simple database with one table, that has various properties.

I am using the latest .NET core 6 with the latest EF (6.0.4)

I wanted to scaffold my database so it generates models, so I run the command:

dotnet ef dbcontext scaffold "Server=MyServerName;Database=MyDBName;User Id=sa;Password=abc123;" 
Microsoft.EntityFrameworkCore.SqlServer -o Model --no-pluralize -d -f

the -d command is supposed to use data annotations rather than fluent annotation, however it does not. I end up with a MyTable class, with the properties listed plainly, e.g. public int MyTableId; public string MyTableProperty;.... and then in my MyDBNameContext class OnModelCreating method, several pages of stuff like this:

 modelBuilder.Entity<MyTable>(entity =>
            {
                entity.HasIndex(e => e.SomeColumn, "idx_somecolumn");

                entity.Property(e => e.firstcolumn)
                    .HasColumnType("decimal(8, 1)")
                    .HasColumnName("FirstColumn");

                ...

This is pretty hideous. The help states this:

-d|--data-annotations Use attributes to configure the model (where possible). If omitted, only the fluent API is used.

I could painstakingly go through manually and add the annotations but then if I regenerate in the future due to changes, then it will undo my work.

Is there a reason why the data annotations attribute is not working?


Solution

  • I can't delete my question so I'll just post the answer here...

    It's a bug!

    -d is not working, but --data-annotations is working.

    https://github.com/dotnet/efcore/issues/26687