Entity Framework Core SQL Server reverse engineering a.k.a Database First gotchas (and workarounds)
This post lists some of known issues you may encounter with Entity Framework Core Power Tools SQL Server reverse engineering or when running the dotnet ef dbcontext scaffold
command, and provides resolutions / workarounds for the issue.
Timeouts with GetIndexes method when reverse engineering database with many indexes
Issue
If you have a SQL Server database with many thousands of index columns, you may see timeout errors with the EF Core command line tools.
Workarounds
- Use EF Core Power Tools.
- Try updating statistics on the sys tables
- Try clearing the procedure cache with
DBCC FREEPROCCACHE
- Add "Command Timeout=300" to your connection string
SQL Server default values and computed column definitions are missing in the generated code
Issue
Expected values for HasDefaultValueSql and HasComputedColumnSql are not generated, this is caused by the account running the scaffolding commands having limited rights to view definitions, as designed.
Run this SQL statement to confirm: SELECT HAS_PERMS_BY_NAME(DB_NAME(), 'DATABASE', 'VIEW DEFINITION')
Workarounds
- Use EF Core Power Tools, and be warned if the user account used for scaffolding does not have the required rights.
- Grant the required rights to the user account used -
GRANT VIEW DEFINITION to limited_user;
Cannot scaffold tables with blank column names
Issue
SQL Server allows blank column names in tables, but this causes the following error when scaffolding: The string argument 'originalIdentifier' cannot be empty.
Workarounds
- Use EF Core Power Tools, which contains a fix for this. (Fix is also in EF Core 6.0)
- Rename the column :-)
When using the EF Core 5 or later command line tools, pluralization is suddenly enabled
Issue
With EF Core 5.0, pluralization using the Humanizer library is enabled by default. This causes unexpected name changes if you use EF Core 5.0 scaffolding on an upgraded project.
Workaround
To revert to the previous behavior, use the new --no-pluralize
(dotnet) / -NoPluralize
(PMC) command line option
Build warning: RelationalIndexBuilderExtensions.HasName(IndexBuilder, string)' is obsolete
Issue
You get the following build warning after upgrading a project with a scaffolded DbContext to EF Core 5.0: Warning CS0618 'RelationalIndexBuilderExtensions.HasName(IndexBuilder, string)' is obsolete: 'Use HasDatabaseName() instead.
Workaround
Re-run scaffolding with EF Core 5 tools.
BIT NOT NULL with DEFAULT 1 is generated as bool?
Issue
This is actually by design, if you think this is the wrong behavior, remove the default. See a related issue and the explanation from the EF Core team
Error message from EF Core Power Tools: Unable to launch 'dotnet' version 3 or newer.
Issue
The latest version of EF Core Power Tools relies on the presence of the .NET Core x64 run-time on the developer machine. If the run-time is not installed, you will get the above error message.
Workaround