从接口继承的列名“Discriminator”无效

时间:2012-12-21 08:31:09

标签: asp.net-mvc-3 entity-framework-4 ef-code-first

我需要你的帮助。每次进行选择时,我总是会收到错误“无效的列名'判别器'”。有点奇怪的是,我的映射或表格中没有任何Discriminator列。我尝试在我的班级中添加[NotMapped](如此处所述:EF Code First “Invalid column name 'Discriminator'” but no inheritance),但无济于事。

以下是触发错误的代码。

模型

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
}

接口

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

配置类

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();
        }
    }
}

选择方法

    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;
    }
希望你能帮助我。非常感谢!

1 个答案:

答案 0 :(得分:1)

我在稍微不同的情况下遇到了同样的错误,我正在使用继承。通过确保可以从模型类访问的所有表/链接由您的上下文配置来解决该问题。

在您的情况下,这是UserAccess和UserAccessDetail类(以及可以从它们到达的任何其他内容)。尝试在OnModelCreating中配置它们。