.Net Core 5 Overwriting SQL Server Default Value

PostAlmostAnything 1 Reputation point
2021-09-05T02:21:50.513+00:00

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.

Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-09-06T03:00:49.803+00:00

    Hi @PostAlmostAnything ,

    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

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.