0

Iam working with Entity Framework Core and a legacy database.

When migrating the database to my models, Iam using this script:

Scaffold-DbContext "Server=abc\SQLEXPRESS;Database=mydatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Everything works fine but the script is creating a class with a property that is nullable:

public bool? Active { get; set; }

But my database has a field, which is "Not Null"

The migration script is also creating a context file:

entity.Property(e => e.Active)
                    .IsRequired()
                    .HasColumnName("active")
                    .HasDefaultValueSql("((1))");

Why is the migrated field "Active" a nullable bool? and not just a bool in my C# models?

1 Answer 1

2

I think it's because you have a default value. In this case, EF accepts null value because the DB server puts the default one. In my experience, "Not null" field without default value render non nullable bool field in my model.

Sign up to request clarification or add additional context in comments.

1 Comment

Jep, this is the reason

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.