2

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);
    }
}
EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40
Sudarshan Tanuku
  • 191
  • 1
  • 11

2 Answers2

0

It was a weird situation, the references to the project that has my pocos were out of sync and were not getting rebuilt when I built my solution. One of the project is a Workflow console app targeting x86 platform. I changed to AnyCPU for all the projects in my sln. And magically ot worked again.

I was trying to add a new column/property to ine of my poco entity.

Sudarshan Tanuku
  • 191
  • 1
  • 11
0

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. :)

Slothario
  • 2,830
  • 3
  • 31
  • 47