question

PostAlmostAnything-1364 avatar image
0 Votes"
PostAlmostAnything-1364 asked ZhiLv-MSFT answered

.Net Core 5 Overwriting SQL Server Default Value

I have a custom column in my AspNetUsers table called IsApproved with a Default Value or Binding of ((1)) which should result in the value of every new record being True, but for some reason .Net Core appears to be creating new records with a value of False. I noticed this after extending the default IdentityUser class using a custom ApplicationUser.

The only thing I added to the ApplicationUser class related to the IsApproved column is one line of code in ApplicationUser.cs which is as follows:

public bool IsApproved { get; set; }

I use the default Identity pages for creating users. Since I added no code to handle that column the value passed to the database should be null and then the database should change that null to True based on the default value, but that is not happening.

dotnet-entity-framework-coredotnet-aspnet-core-razor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The default value of a bool is false not null. Use a nullable type or set the default value of the IsApproved property.

 public bool? IsApproved { get; set; }


 public bool IsApproved { get; set; } = true




0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @PostAlmostAnything-1364,

The only thing I added to the ApplicationUser class related to the IsApproved column is one line of code in ApplicationUser.cs which is as follows:

  public bool IsApproved { get; set; }

By using the above code, the 'bool' property 'IsApproved' on entity type 'ApplicationUser' is configured with a database-generated default. This default will always be used for inserts when the property has the value 'false', since this is the CLR default for the 'bool' type. So, when insert the new entity, the default value is false.

To set the default value when using EF Core, you can configure a default value on a property:

     protected override void OnModelCreating(ModelBuilder builder)
     {
         base.OnModelCreating(builder);
         builder.Entity<ApplicationUser>()
             .Property(b => b.IsApproved)
             .HasDefaultValue(true);
     }

Then, use the Add-Migration and Update-Database command to enable migration and update the database.

After that, you can check the data table designer via the SQL Server Object Explorer, like this:

129445-image.png

Then, after inserting new user, the default value will be True:

129378-image.png

[Note] The existing record's IsApproved value will not change it. You need to change it by yourself. You can change them via the SQL Server Object Explorer and the AspNetUsers's data view or create a SQL query, refer the follow screenshot:

129397-57.gif


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Dillion



image.png (86.8 KiB)
image.png (38.5 KiB)
57.gif (732.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.