Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot insert the value NULL into column 'Discriminator', table '...AspNetUsers'; column does not allow nulls. INSERT fails

Entity Framework 6, MVC5, ASP Identity 2.0.

I'm new to Entity framework and ASP Identity. I've created database with Code First approach, following these instructions: http://dotnetcodr.com/2014/07/10/introduction-to-entityframework-6-part-1-the-basics-of-code-first/ and applying them accordingly to my own project.

Tables were created, all fine. But when I'm trying to Register a new user, exception is thrown: "Entity Framework 6, ASP Identity. Cannot insert the value NULL into column 'Discriminator', table '...AspNetUsers'; column does not allow nulls. INSERT fails." Triggered by CreateAsync method within AccountController

Line 153: {
Line 154: var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
Line 155: var result = await UserManager.CreateAsync(user, model.Password);
Line 156: if (result.Succeeded)
Line 157: {

I'm not even sure which part of code I should paste here.

Here is my DbContext:

    public class BulkMailerContext : IdentityDbContext<ApplicationUser>
{
    public BulkMailerContext()
        : base("BulkMailerContext", throwIfV1Schema: false)
    {
    }
    public DbSet<Email> Emails { get; set; }
    public DbSet<SubscriptionList> SubscriptionLists { get; set; }
    public DbSet<Recipient> Recipients { get; set; }
}

And here is default Identity context:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("BulkMailerContext", throwIfV1Schema: false)
    {
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

I haven't found much information about this exception.

This post http://forums.asp.net/t/1974183.aspx?Cannot+insert+the+value+NULL+into+column+Discriminator suggests that project has a class, inheriting from User. I don't have anything like that.

This post https://connect.microsoft.com/VisualStudio/feedback/details/800655/calling-new-user-with-4-5-1-does-not-pass-column-discriminator-required-in-aspnetusers-tables is about Web API project and Microsoft team said it's a bug, and they will fix it, as far as I got it

This one Cannot insert the value NULL into column 'Discriminator' AspNetUsers Table doesn't offer any solution

This one Web API 2 cannot register user suggests that in Identity 2.0 this has been fixed, but I have Identity 2.0 currently

I have automatic migrations - turned off, if it matters. But I've updated db manually from PM console with update-database command.

Any suggestions at all are welcome.

like image 202
Valeriya Avatar asked Dec 14 '14 14:12

Valeriya


1 Answers

I ran "add-migration -ConfigurationTypeName ...ApplicationContextMigrations.Configuration "InitialCreate” -force"

Then, deleted all Identity tables from database.

Then ran "update-database ...ApplicationContextMigrations.Configuration"

And Discriminator column disappeared. User is registered successfully, when I run application.

like image 169
Valeriya Avatar answered Nov 15 '22 11:11

Valeriya