2

I need your help on this. I always get the error "Invalid column name 'Discriminator'" every time I do a select. What's weird is that, I don't have any Discriminator column in my mapping or in my table. I tried adding the [NotMapped] in my class (as mentioned here: EF Code First “Invalid column name 'Discriminator'” but no inheritance) but to no avail.

Below are my codes which triggered the error.

Model

public class MasterUser : IAuditFields
{
    [Key]
    public string Username { get; set; }
    public string Name { get; set; }
    public string GroupID { get; set; }
    public string Status { get; set; }
    public string DeptID { get; set; }
    public string Rank { get; set; }

    public string CreatedBy { get; set; }

    [Required]
    public DateTime CreatedDate { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTime? LastModifiedDate { get; set; }

    #region Relationships

    public UserAccess UserAccess { get; set; } // User needs to see what user access group he/she is in
    public UserAccessDetail UserAccessDetail { get; set; } //User needs to see what he/she can do
    #endregion
}

Interface

public interface IAuditFields
{
    string CreatedBy { get; set; }
    DateTime CreatedDate { get; set; }
    string LastModifiedBy { get; set; }
    DateTime? LastModifiedDate { get; set; }
}

Configuration class

public class MasterUserConfig : EntityTypeConfiguration<MasterUser>
{
    public MasterUserConfig()
    {
        Property(usr => usr.Username)
            .HasColumnName("UserName")
            .IsRequired();
        Property(usr => usr.Name).HasColumnName("Name");
        Property(usr => usr.GroupID).HasColumnName("GroupId");
        Property(usr => usr.Status).HasColumnName("Status");
        Property(usr => usr.DeptID).HasColumnName("DeptId");
        Property(usr => usr.Rank).HasColumnName("Rank");
        Property(usr => usr.CreatedBy).HasColumnName("CreatedBy");
        Property(usr => usr.CreatedDate).HasColumnName("CreatedDate");
        Property(usr => usr.LastModifiedBy).HasColumnName("LastModifiedBy");
        Property(usr => usr.LastModifiedDate).HasColumnName("LastModifiedDate");

        ToTable("dbo.Users");
    }

DataContext

public class BBDataContext : DbContext
{
    public DbSet<MasterUser> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        try
        {
            modelBuilder.Configurations.Add(new MasterUserConfig());

            base.OnModelCreating(modelBuilder);
        }
        catch (Exception ex)
        {
            DiagnosticHelper.Message = ex.Message;
            DiagnosticHelper.InnerException = ex.InnerException.Message;
            DiagnosticHelper.StackTrace = ex.StackTrace;
            DiagnosticHelper.Instance.WriteError();
        }
    }
}

Select method

    public MasterUser GetUserByUsername(string userName, string password)
    {
        try
        {
            return (_context.Users
                .Where(usr => usr.Username == userName && usr.Password == password && usr.Status == "ACTIVE"))
                .SingleOrDefault();
        }
        catch (Exception ex)
        {
            LogError(ex);
        }
        return null;
    }

Hope you can help me. Thanks a lot!

Community
  • 1
  • 1
Musikero31
  • 3,115
  • 6
  • 39
  • 60

1 Answers1

1

I've had the same error under slightly different circumstances, I was using inheritance. The problem was fixed by ensuring that all tables/links that can be reached from the model class are configured by your context.

In your case this is the UserAccess and UserAccessDetail classes (and anything else that can be reached from them). Try configuring these in OnModelCreating.

MarkB
  • 174
  • 14