DBContext Method not found (System.MissingMethodException on OnModelCreating())

ef-code-first ef-code-first-mapping entity-framework-6

Question

I've been stuck with below exception from OnModelCreating() in DBContext and struggling to find the cause or a solution. Can't find much help online either.

 {System.MissingMethodException: Method not found: 'System.Nullable`1<Int32> Models.Application.get_AddrSeqNum()'. at Data.Configuration.ApplicationConfiguration..ctor()

Any help or ideas please?

Details:

I've two entity types 'Application' and 'Organization' and their Entity Type Configurations as below:

public  class Application
{
   public int ApplId { get; set; }
   public int? OrganizationId { get; set; } // Compsite FK (maps to ExternalId in Organization)
   public int? AddrSeqNum { get; set; } // Compsite FK (maps to AddrSeqNum)
   public virtual Organization Organization { get; set; }     
}

public class Organization
{       
    public int Id { get; set; }
    /// <summary>
    /// Organization ID
    /// </summary>
    public int ExternalId { get; set; }
    public int AddrSeqNum { get; set; }        
}

public class ApplicationConfiguration : EntityTypeConfiguration<Application>
{
    ToTable("Application");            

    HasKey(e => e.ApplId)
      .Property(e => e.ApplId)
      .HasColumnName("appl_id")
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

     Property(e => e.AddrSeqNum)
          .HasColumnName("addr_seq_num");

     // Relationship
     HasOptional(e => e.Organization)
            .WithMany()
            .HasForeignKey(e => new { e.OrganizationId, e.AddrSeqNum });
}

public class OrganizationConfiguration : EntityTypeConfiguration<Organization>
{
    public OrganizationConfiguration()
    {
        ToTable("Organization");

        HasKey(e => new { e.ExternalId, e.AddrSeqNum }); // Compsite Unique key in table

        Property(e => e.ExternalId)
            .HasColumnName("external_id");

        Property(e => e.AddrSeqNum)
           .HasColumnName("addr_sequence");

        Property(e => e.Id)
            .HasColumnName("Id") // Primary key (auto increment)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

DBContext:

public partial class AppDbContext : DbContext
{
    public AppDbContext ()
    {
        Configuration.LazyLoadingEnabled = true;
        Configuration.ProxyCreationEnabled = false;
        //Configuration.AutoDetectChangesEnabled = false;

        DbInterception.Add(new FtsInterceptor());
        Database.Log = (msg) => Logger.Debug(msg);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Configurations  // <<- Exception from here
            .Add(new ApplicationConfiguration())                
            .Add(new OrganizationConfiguration())
        base.OnModelCreating(modelBuilder);
    }
}
1
2
9/24/2019 4:31:46 PM

Popular Answer

I was having this problem too, because I made a nullable database column non-nullable and Visual Studio didn't seem to want to cooperate with the change.

Deleting bin and obj folders, as well as rebuilding, didn't seem to do it.

As a workaround, I just made it nullable again. It's interesting because I'm not sure where VS is caching the nullable part--a question to be answered when I'm under less tight deadlines. :)

0
4/13/2017 10:31:21 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow