I had this problem for a very long time and I don't know how to solve this. I always get the error "Invalid column name 'Discriminator'" but I don't have a discriminator column. I've configured it correctly in my data context but it still throws the same
error.
Below are the codes for your reference
User model
public class MasterUser : IAuditFields
{
[Key]
public string Username { get; set; }
public string Name { get; set; }
public string AltName { get; set; }
public string GroupID { get; set; }
public string Status { get; set; }
public string DeptID { get; set; }
public string Rank { get; set; }
public int FailCount { get; set; }
public string ChangePassword { get; set; }
public string CompanyCode { get; set; }
public string CountryCode { get; set; }
public string ServiceID { get; set; }
[Required]
[Display(Name = "Password: ")]
[DataType(DataType.Password)]
public string Password { get; set; }
[RegularExpression(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public string IsPermanentStaff { get; set; }
[Required]
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string LastModifiedBy { get; set; }
public DateTime? LastModifiedDate { get; set; }
public DateTime LastPasswordChange { get; set; }
#region Relationships
[ForeignKey("GroupID")]
public UserAccess UserAccess { get; set; } // User needs to see what user access group he/she is in
[ForeignKey("GroupID")]
public UserAccessDetail UserAccessDetail { get; set; } //User needs to see what he/she can do
#endregion
}
The thing is, it still works before even if it is in 2000.
That's weird...
After trying that re-engineer tool, it seems that the tool only supports SQL Server 2005 and later. But using the code-first approach to SQL Server 2000 still works. But you need to do it manually.
Musikero11
Member
333 Points
182 Posts
Invalid column name Discriminator, in Code First
Dec 27, 2012 09:41 AM|LINK
I had this problem for a very long time and I don't know how to solve this. I always get the error "Invalid column name 'Discriminator'" but I don't have a discriminator column. I've configured it correctly in my data context but it still throws the same error.
Below are the codes for your reference
User model
public class MasterUser : IAuditFields { [Key] public string Username { get; set; } public string Name { get; set; } public string AltName { get; set; } public string GroupID { get; set; } public string Status { get; set; } public string DeptID { get; set; } public string Rank { get; set; } public int FailCount { get; set; } public string ChangePassword { get; set; } public string CompanyCode { get; set; } public string CountryCode { get; set; } public string ServiceID { get; set; } [Required] [Display(Name = "Password: ")] [DataType(DataType.Password)] public string Password { get; set; } [RegularExpression(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$")] [DataType(DataType.EmailAddress)] public string Email { get; set; } public string IsPermanentStaff { get; set; } [Required] public string CreatedBy { get; set; } public DateTime CreatedDate { get; set; } public string LastModifiedBy { get; set; } public DateTime? LastModifiedDate { get; set; } public DateTime LastPasswordChange { get; set; } #region Relationships [ForeignKey("GroupID")] public UserAccess UserAccess { get; set; } // User needs to see what user access group he/she is in [ForeignKey("GroupID")] 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; } }User model configuration
public class MasterUserConfig : EntityTypeConfiguration<MasterUser> { public MasterUserConfig() { Property(usr => usr.Username) .HasColumnName("MUSR_USRID") .IsRequired(); Property(usr => usr.Name).HasColumnName("MUSR_ENAME"); Property(usr => usr.AltName).HasColumnName("MUSR_ANAME"); Property(usr => usr.GroupID).HasColumnName("MUSR_GRPID"); Property(usr => usr.Status).HasColumnName("MUSR_STAT"); Property(usr => usr.DeptID).HasColumnName("MUSR_DEPT"); Property(usr => usr.Rank).HasColumnName("MUSR_RANK"); Property(usr => usr.FailCount).HasColumnName("MUSR_FLCNT"); Property(usr => usr.ChangePassword).HasColumnName("MUSR_CHGPW"); Property(usr => usr.CompanyCode).HasColumnName("MUSR_COMCD"); Property(usr => usr.CountryCode).HasColumnName("MUSR_CTRCD"); Property(usr => usr.ServiceID).HasColumnName("MUSR_SVCID"); Property(usr => usr.Password).HasColumnName("MUSR_PWD"); Property(usr => usr.Email).HasColumnName("MUSR_EMAIL"); Property(usr => usr.IsPermanentStaff).HasColumnName("MUSR_PSTFF"); Property(usr => usr.CreatedBy).HasColumnName("MUSR_CUSER"); Property(usr => usr.CreatedDate).HasColumnName("MUSR_CREDT"); Property(usr => usr.LastModifiedBy).HasColumnName("MUSR_LUSER"); Property(usr => usr.LastModifiedDate).HasColumnName("MUSR_LSTDT"); Property(usr => usr.LastPasswordChange).HasColumnName("MUSR_LSTPWCHNGE"); ToTable("dbo.MUSR_FIL"); }DataContext
public class BBDataContext : DbContext { public DbSet<MasterUser> Users { get; set; } public DbSet<LabelMessageModel> LabelMessageModels { get; set; } public DbSet<UserAccessDetail> UserAccessDetails { get; set; } public DbSet<Function> Functions { get; set; } public DbSet<UserAccess> UserAccesses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { try { modelBuilder.Configurations.Add(new MasterUserConfig()); modelBuilder.Configurations.Add(new LabelConfig()); modelBuilder.Configurations.Add(new UserAccessConfig()); modelBuilder.Configurations.Add(new UserAccessDetailConfig()); modelBuilder.Configurations.Add(new FunctionConfig()); base.OnModelCreating(modelBuilder); } catch (Exception ex) { DiagnosticHelper.Message = ex.Message; DiagnosticHelper.InnerException = ex.InnerException.Message; DiagnosticHelper.StackTrace = ex.StackTrace; DiagnosticHelper.Instance.WriteError(); } } }User model query
public class LoginQuery : LinQHelperClass { #region Declarations BBDataContext _context = new BBDataContext(); #endregion #region Public Methods /// <summary> /// Retrieves the user from username and password /// </summary> /// <param name="userName">Username </param> /// <param name="password">Hashed password</param> /// <returns></returns> 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; } #endregion #region Private Methods private static void LogError(Exception ex) { DiagnosticHelper.Message = ex.Message; if (ex.InnerException != null) { DiagnosticHelper.InnerException = ex.InnerException.Message; } DiagnosticHelper.StackTrace = ex.StackTrace; DiagnosticHelper.Instance.WriteError(); } #endregion }Musikero by night Musician's blog.
Don't forget to mark as "Answer" if that helps.
sameer_khanj...
Contributor
7056 Points
1376 Posts
Re: Invalid column name Discriminator, in Code First
Dec 27, 2012 09:53 AM|LINK
Please check , hopefully it will solve your problem
http://stackoverflow.com/questions/6553935/ef-code-first-invalid-column-name-discriminator-but-no-inheritance
sameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
farooque84
Participant
1358 Points
321 Posts
Re: Invalid column name Discriminator, in Code First
Dec 27, 2012 09:54 AM|LINK
Hi,
The solution is quite simple and you just need to add [NotMapped] as an attribute of the derived class.
Just refer this below URL,
http://stackoverflow.com/questions/6553935/ef-code-first-invalid-column-name-discriminator-but-no-inheritance
Musikero11
Member
333 Points
182 Posts
Re: Invalid column name Discriminator, in Code First
Dec 27, 2012 02:33 PM|LINK
Actually, it's not a derived class. This class represents the "dbo.MUSR_FIL" table in my database. That's why I need to map it.
Musikero by night Musician's blog.
Don't forget to mark as "Answer" if that helps.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Invalid column name Discriminator, in Code First
Dec 28, 2012 12:26 AM|LINK
Hi,
Are you using Code-first to an existing table? Or a Db-first?
Musikero11
Member
333 Points
182 Posts
Re: Invalid column name Discriminator, in Code First
Dec 28, 2012 01:02 AM|LINK
I am using code-first to an existing table because my database is created using SQL Server 2003
Musikero by night Musician's blog.
Don't forget to mark as "Answer" if that helps.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Invalid column name Discriminator, in Code First
Dec 28, 2012 01:13 AM|LINK
2003?
I only know about 2000, 2005 and 2008, the latest version is 2012.
But for mapping to an existing db's table, you can refer this:
http://msdn.microsoft.com/en-us/data/jj200620.aspx
Musikero11
Member
333 Points
182 Posts
Re: Invalid column name Discriminator, in Code First
Dec 28, 2012 02:46 AM|LINK
I tried this one but it does not support SQL Server 2000 (Yes, my mistake. It's SQL Server 2000).
Musikero by night Musician's blog.
Don't forget to mark as "Answer" if that helps.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Invalid column name Discriminator, in Code First
Dec 28, 2012 03:27 AM|LINK
Sorry 2000 isn't supported by Code-first, at least you should upgrate to 2005, 2008 or 2010.
Musikero11
Member
333 Points
182 Posts
Re: Invalid column name Discriminator, in Code First
Dec 28, 2012 05:15 AM|LINK
The thing is, it still works before even if it is in 2000.
That's weird...
After trying that re-engineer tool, it seems that the tool only supports SQL Server 2005 and later. But using the code-first approach to SQL Server 2000 still works. But you need to do it manually.
Musikero by night Musician's blog.
Don't forget to mark as "Answer" if that helps.