Timothy Hunt
Timothy Hunt

Reputation: 31

How do I set a default value on a bit field using Entity Framework Core code-first?

Here is my code:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Skill>()
        .Property(s => s.FlagTrainedOnly)
        .HasDefaultValue(false);
}

And the error I get:

Error CS1061
'PropertyBuilder' does not contain a definition for 'HasDefaultValue' and no extension method 'HasDefaultValue' accepting a first argument of type 'PropertyBuilder' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 3

Views: 2737

Answers (1)

Glyn
Glyn

Reputation: 339

Make sure you've got

using Microsoft.EntityFrameworkCore;

at the top and then try putting it on one line like this

protected override void OnModelCreating(ModelBuilder modelBuilder) {
    modelBuilder.Entity<Skill>().Property(p => p.FlagTrainedOnly).HasDefaultValue(false);
}

Then put it on multiple lines. I think sometimes it needs to be reminded that the dot extensions exist.

Upvotes: 2

Related Questions