1

I'm generating my Entity Framework C# classes using a database-first approach, and running the following in the Package Manager console:

Scaffold-DbContext "Server=[...];Database=[...];Integrated Security=True;Trusted_Connection=True;encrypt=false;TrustServerCertificate=true;"
    Microsoft.EntityFrameworkCore.SqlServer 
    -Project Proj.EF -StartupProject Proj.EF.Updator 
    -Context Db4eeBaseContext -Force -OutputDir Db4_EE -UseDatabaseNames

I have .NET Core 2.1.2 installed and am running Visual Studio 2022, version 17.13.7.

We have columns in the database that are NOT NULL, yet have default values. The scaffolding makes these nullable properties, and shows this warning:

The column 'dbo.T_PERSON_RIDP.IS_ACTIVE' would normally be mapped to a non-nullable bool property, but it has a default constraint. Such a column is mapped to a nullable bool property to allow a difference between setting the property to false and invoking the default constraint.

Is there a parameter I can pass to Scaffold-DbContext to have it NOT create nullable properties?

Reason: we are upgrading our EF code from old .edmx to new EF Core 6 and want our new database-first C# classes to be created the exact same way in order to make the migration easier. And removing those Default() values in the database isn't an option we can go with.

5
  • you can Manually update .cs classes after generation. Commented May 23 at 19:29
  • 1
    Unfortunately, this is the behaviour until EF Core 8 Commented May 25 at 14:06
  • 1
    And even after EF Core 8, there is a catch. Commented May 25 at 14:10
  • 1
    I wouldn't set the expectation to migrate without making any changes. EF Core 2.1 ended support in 2021 so there is that as well... They opted to fix a behaviour limitation/bug with EF where an entity with a non-nullable Boolean with a default could not both support insert with a False value, and support insert with a defaulted value. The best solution is to bite that bullet and adjust your domain to work with the Required null-able Booleans. Commented May 25 at 21:09
  • 1
    EF Core intentionally scaffolds columns with NOT NULL and DEFAULT constraints as nullable to allow distinguishing between "unset" and "explicitly set" values. Currently, there's no flag in Scaffold-DbContext to override this. A workaround is to post-process the generated files or use tools like EF Core Power Tools for more control. Commented May 27 at 8:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.