简体   繁体   中英

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, eg public int MyTableId; public string MyTableProperty;.... 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?

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

You can use this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM