Dotnet Core 3.1 - Updating to 3.1 and setting default values using "HasDefaultValue" doesnt seem to exist

c# entity-framework-core


Updating to Dotnet Core 3.1 and porting my code I find that in my DbContext I am getting the following error:

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

My code where this occurs follows:

        modelBuilder.Entity<Tenant>().Property(t => t.TenantNo).HasMaxLength(20);
        modelBuilder.Entity<Tenant>().Property(t => t.CompanyName).HasMaxLength(100).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.ContactLastName).HasDefaultValue(false).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.Email).HasMaxLength(500).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.MobilePhone).HasMaxLength(20).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.OfficePhone).HasMaxLength(20);
        modelBuilder.Entity<Tenant>().Property(t => t.CompanyEmail).HasMaxLength(500);
        modelBuilder.Entity<Tenant>().Property(t => t.Address1).HasMaxLength(500);
        modelBuilder.Entity<Tenant>().Property(t => t.Address2).HasMaxLength(500);
        modelBuilder.Entity<Tenant>().Property(t => t.ABN).HasMaxLength(14);
        modelBuilder.Entity<Tenant>().Property(t => t.Database).HasMaxLength(100).IsRequired();
        modelBuilder.Entity<Tenant>().Property(t => t.IsLocked).HasDefaultValue(false);

Where ever I have used .HasDefaultValue I am getting this error. I have, I believe, all the required directives...

using JobsLedger.CATALOG.Entities;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using System.Threading.Tasks;

Seems in upgrading from 3.0 to 3.1 they missed this out or they are using a different way of setting default values..

So before I posted this I did do a google search and also a Stackoverflow search with no results.

Wondering someone might suggest how to set default values in 3.1?

12/8/2019 9:36:15 AM

Popular Answer

Have you added that package in your project for the 3.1 core?

I suggest that you install and/or remove/install the package and see how this works.

Nothing has changed in EF 3.1 as you can see in the changelog here

To this end we have fixed over 150 issues for the 3.1 release, but there are no major new features to announce.

By the way, this is the current code for 3.1 in the package, where you can see the existance of the extention.

    public static PropertyBuilder HasDefaultValue(
        [NotNull] this PropertyBuilder propertyBuilder,
        [CanBeNull] object value = null)
        Check.NotNull(propertyBuilder, nameof(propertyBuilder));

        propertyBuilder.Metadata.SetDefaultValue(value ?? DBNull.Value);

        return propertyBuilder;
12/8/2019 9:42:22 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow