0

I have a WebForms applicaiton using the new ASP.NET Identity. I've added a couple of additional fields in the class to allow for Email and a Boolean called IsOnLine.

I use migrations, to explicititly update the tables, and can see that my new fields are there. However, whenever I try to login now i get the following error:

Additional information: The 'IsOnLine' property on 'ApplicationUser' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.Boolean'.

All the exmamples on the net relate to MVC, which i'm not using yet.

How can i fix this?

2 Answers 2

1

A bool cannot be null so that is what is set to in the database. If you want it to be null you will need to define it as a nullable bool using the ? operator.

public class ApplicationUser : IdentityUser
{
    public bool? IsOnline { get; set; }
}

To update this information in the database take a look at this article on adding email confirmation to ASP.NET Identity. In this example there is a boolean flag IsConfirmed that is updated during the registration process, which will be similar to your IsOnline flag. Here is a snippet of the relevant code to update the database.

    user.IsOnline = true;
    DbSet<ApplicationUser> dbSet = context.Set<ApplicationUser>();
    dbSet.Attach(user);
    context.Entry(user).State = EntityState.Modified;
    context.SaveChanges();
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, so that fixed that. Next question. How do i set the bool to be true once the user has been authenticated?
0

I prefer to avoid null values whenever possible, to determine if a property can accept nulls values it is better to think what that information represents in the domain problem. In this case i think it not make sense to have an undetermined value for IsOnline because the user is online or offline, not middle terms apply to this fact. To resolve this problem with the database, taking advantage that you are using Code Migration , you can use the Seed method from Configuration class and assing the value(s) to your new fields that are not nullable.

Pseudo code:

Seed() 
    for each user
        set user IsOnline to false

Comments

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.