I have a custom initializer setup as follows:
public class PromptIfChangesNeededDBInitializer : IDatabaseInitializer<MeyerREContext>
{
public PromptIfChangesNeededDBInitializer()
{ // This constructor is called properly
}
#region IDatabaseInitializer<TContext> Members
public void InitializeDatabase(MeyerREContext context)
{ // This is never called
... Code that checks existence and seeds etc
}
}
Here is my DbContext class
public class MeyerREContext : DbContext
{
static MeyerREContext()
{
Database.SetInitializer(new PromptIfChangesNeededDBInitializer());
}
public DbSet<Address> Addresses { get; set; }
... More DbSet property definitions
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AddressMap());
... More Configurations
}
}
This is the first call to the context
City city = dbNew.Cities.Where(e=>e.CityName=="Foley").FirstOrDefault();
The constructor of the Initializer is called properly as confirmed by a breakpoint, the OnModelCreating runs properly as confirmed by a breakpoint, but after the OnModelCreating is completed the InitializeDatabase is never called…
I removed the Database.SetInitializer(new PromptIfChangesNeededDBInitializer()) call from the ctor of the DBContext into the ctor of the calling class BEFORE any calls to the context and I get slightly different behavior now:
public class CreateData
{
private VFPModelContainer db = new VFPModelContainer();
private MeyerREContext dbNew;
public CreateData()
{
Database.SetInitializer(new PromptIfChangesNeededDBInitializer<MeyerREContext>());
dbNew = new MeyerREContext();
dbNew.Database.Initialize(force: true); NUll Exception here now...
}
A null exception is geenrated now in the EF Framework code:
Here is the exception detail:
System.NullReferenceException occurred
Message=Object reference not set to an instance of an object.
Source=EntityFramework
StackTrace:
at
System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.ValidateConsistency(NavigationPropertyConfiguration navigationPropertyConfiguration)
InnerException:
It looks like the exception is happening in one of the EntityTypeConfiguration calls, but the exception is being swallowed somehow ? How can I figure out which call it is ? There are around 100 entities in this model…

How to get more info about what went wrong inside EF ? I need to know which navigational property is having problems… I think the problem is that EF is creating background worker threads to create & validate the model, but I don’t understand why the exception is soooo vague ??
Any ideas ?
Thanks
Greg
had to install Reflector and Decompile the EF Assembly to find out what the problem was:
This is the line that causes the problem…
In my case the InverseNavigationProperty property is null which causes and exception WHEN EF is trying to throw an exception…
The underlying problem is that I had an inverse property relationship defined but no mapping defined for it…
However, it was impossible to acertain which entity was causing this problem… These checks in this method should be wrapped with a try catch to avoid this problem so they can throw a more meaningful error to the end user…
Thanks
Greg