Search code examples
c#entity-frameworkef-code-first

The property is not a declared property on type 'class'


I got the error The property 'Rank' is not a declared property on type 'MasterUser'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property. I checked my model class and it is there.

public class MasterUser
{
    //many other properties...
    public virtual char Rank { get; set; }
    //more properties below
}

I also checked my db context and it is also there and mapped perfectly.

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        #region MUSR_USRID mapping

        //Mapped properties above....
        modelBuilder.Entity<MasterUser>()
            .Property(usr => usr.Rank).HasColumnName("MUSR_RANK");
        //More mapped properties..

        modelBuilder.Entity<MasterUser>().ToTable("dbo.MUSR_FIL");
        #endregion

        base.OnModelCreating(modelBuilder);
    }
}

In the table, MUSR_RANK is nullable with a char datatype.

How can I fix this? Does the database have an effect on this? I am currently using SQL Server 2000.

Thanks for the help.


Solution

  • Primitive Data Types Supported in the Entity Data Model

    So you have to use a string instead of a char, I think

    and add some datas in OnModelCreating

    modelBuilder.Entity<MasterUser>()
                .Property(usr => usr.Rank).HasColumnName("MUSR_RANK")
                .HasMaxLength(1)
                .IsFixedLength()
                .IsUnicode(false);