locked
'AppDbContext' does not contain a definition for 'InstructionalMaterials' RRS feed

  • Question

  • User1280950372 posted

    I get the above error while building my project yet the code below clearly shows that InstructionMaterials does exist in the EF context class. Any ideas?

    modelBuilder.Entity<InstructionalMaterials>(entity =>
    {
    	entity.HasKey(e => e.InstructionalMaterialId)
    		.HasName("PK_InstructionalMaterials");
    
    	entity.ToTable("InstructionalMaterials", "DIMS");
    
    	entity.Property(e => e.Copyright)
    		.HasColumnType("datetime2(3)")
    		.HasDefaultValueSql("getdate()");
    
    	entity.Property(e => e.Price).HasColumnType("decimal");
    
    	entity.Property(e => e.Status).IsRequired();
    
    	entity.Property(e => e.Title).IsRequired();
    });
    

    I have four different entity items that are doing this.

    Thursday, May 4, 2017 10:39 PM

Answers

  • User-271186128 posted

    Hi muybn,

    The definition of DbModelBuilder. Entity is:

    public virtual EntityTypeConfiguration<TEntityType> Entity<TEntityType>() where TEntityType : class;

    As you can see, I takes no arguments. So I suggest you could use Fluent Api as followings:

    modelBuilder.Entity<InstructionalMaterials>().HasKey(e => e.InstructionalMaterialId).HasName("PK_InstructionalMaterials");
    modelBuilder.Entity<InstructionalMaterials>().ToTable("InstructionalMaterials", "DIMS");
    modelBuilder.Entity<InstructionalMaterials>().Property(e => e.Copyright).HasColumnType("datetime2(3)").HasDefaultValueSql("getdate()");
    modelBuilder.Entity<InstructionalMaterials>().Property(e => e.Price).HasColumnType("decimal");
    modelBuilder.Entity<InstructionalMaterials>().entity.Property(e => e.Status).IsRequired();
    modelBuilder.Entity<InstructionalMaterials>().entity.Property(e => e.Title).IsRequired();
    

    More about Entity Framework Fluent Api, please refer to the following link:

    https://msdn.microsoft.com/en-us/library/jj591617(v=vs.113).aspx

    If you have any other questions, please feel free to contact me any time.

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 5, 2017 2:54 AM