我有一个简单的数据库,有一个表,有各种属性。
我正在使用最新的。net core 6和最新的EF (6.0.4)
我想支撑我的数据库,以便它生成模型,所以我运行命令:
dotnet ef dbcontext scaffold "Server=MyServerName;Database=MyDBName;User Id=sa;Password=abc123;"
Microsoft.EntityFrameworkCore.SqlServer -o Model --no-pluralize -d -f
-d命令应该使用数据注释而不是流畅注释,但是它没有。我最终得到了一个MyTable
类,其属性清楚地列出,例如public int MyTableId; public string MyTableProperty;....
,然后在我的MyDBNameContext
类OnModelCreating
方法中,有几页这样的东西:
modelBuilder.Entity<MyTable>(entity =>
{
entity.HasIndex(e => e.SomeColumn, "idx_somecolumn");
entity.Property(e => e.firstcolumn)
.HasColumnType("decimal(8, 1)")
.HasColumnName("FirstColumn");
...
这太可怕了。帮助说明如下:
-d|——data-annotations使用属性配置模型(如果可能)。如果省略,则只使用fluent API。
我可以煞费苦心地手动完成并添加注释,但如果将来由于更改而重新生成,那么它将撤销我的工作。
是否有数据注释属性不工作的原因?
我不能删除我的问题,所以我就把答案贴在这里…
这是一个bug!
-d
不工作,但--data-annotations
工作。
https://github.com/dotnet/efcore/issues/26687
你可以这样做:
dotnet ef dbcontext scaffold
"Server=MyServerName;Database=MyDBName;User Id=sa;Password=abc123;"
Microsoft.EntityFrameworkCore.SqlServer -o Model --no-pluralize --data-annotations -f