Chitta
Chitta

Reputation: 185

Entity Framework Code First - Invalid column name 'Discriminator1'

We are trying to migrate a simple membership application to Identity 2.0, but we are getting the following error.

Invalid column name 'Discriminator1'.
Invalid column name 'Discriminator1'.
Invalid column name 'Discriminator1'.

I could not find where the problem is as I have followed all the migration steps properly.

Can anyone help find where the problem might be and how to fix it?
Any suggestion is welcome.

We have the following code related the ASP.NET logins.

     [Table("AspNetUserLogins")]
     public partial class AspNetUserLogins : IdentityUserLogin<Guid>
     {

     }



    [Table("AspNetUserClaims")]
    public partial class AspNetUserClaims : IdentityUserClaim<Guid>
    {
    }



    [Table("t_User")]
    public partial class User : IdentityUser<Guid, AspNetUserLogins, AspNetUserInRoles, AspNetUserClaims>
    {   
        [Key]
        [Column("UserId")]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public override Guid Id { get; set; }

        [Column("Username")]
        public override string UserName { get; set; }

        [NotMapped]
        public Guid UserId { get { return base.Id; } }

        public Guid ApplicationId { get; set; }

        public DateTime? CreationDate { get; set; }

        public string Discriminator { get; set; }

        public int? FailedPasswordAnswerAttemptCount { get; set; }

        public DateTime? FailedPasswordAnswerAttemptWindowStart { get; set; }
        ...

Upvotes: 3

Views: 1497

Answers (2)

hasany
hasany

Reputation: 164

If you have Enum in your model and When updated the existing function like below can fix

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<MyModel>()
    ....
    .Map<AnnouncementEvent>(m => 
    m.Requires("Discriminator").HasValue(MyTypeEnum.Event.ToString()))
    ....
}

Upvotes: 0

Dukhabandhu Sahoo
Dukhabandhu Sahoo

Reputation: 1424

The error Invalid column name 'Discriminator1'. is mainly associated with inheritance. By default, when a class inherits a model(i.e the entity/POCO associated with a table) the code first assumes that the inheriting class is mapped to a table in the database. But if this is NOT the case then use the [NotMapped] attribute just above the inheriting class name like follows:

[NotMapped]
public class ContactUsViewModel : Content
{
    public string FirstName { get; set; }

    public string LastName { get; set; }
    ...
}

Or using Fluent API configuration:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Ignore 'ContactUsViewModel' class as it is not mapped to database
    modelBuilder.Ignore<ContactUsViewModel >();
    ...
}

See the similar problem EF Code First "Invalid column name 'Discriminator'" but no inheritance

Upvotes: 3

Related Questions