What can cause EF Code First IDatabaseInitializer InitializeDatabase to not cause a call?

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 context call.

 City city = dbNew.Cities.Where(e=>e.CityName=="Foley").FirstOrDefault(); 

The initializer constructor is called properly, as confirmed by the breakpoint, OnModelCreating is executed correctly, as confirmed by the breakpoint, but after OnModelCreating is complete, database initialization is never called ...

I removed Database.SetInitializer (new PromptIfChangesNeededDBInitializer () call) from ctor DBContext to ctor of the calling class before any context calls, and now I have a slightly different behavior:

 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... } 

An exceptional exception is now provided in the EF Framework code:

Here is a detailed description of the exception:

 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 occurs in one of the EntityTypeConfiguration calls, but is some exception swallowed? How can I determine which challenge it is? There are about 100 objects in this model.

How can I get more info than is shown in the Intellitrace?

How to get more information about what went wrong inside EF? I need to know which navigation property has problems ... I think the problem is that EF creates workflow threads to create and validate the model, but I don’t understand why the exception is soooo undefined ??

Any ideas?

Thanks Greg

+4
source share
1 answer

I had to install Reflector and decompile the EF assembly to find out what the problem was:

This is the line that causes the problem ...

 private void ValidateConsistency(NavigationPropertyConfiguration navigationPropertyConfiguration) { if ((navigationPropertyConfiguration.InverseEndKind.HasValue && this.EndKind.HasValue) && (navigationPropertyConfiguration.InverseEndKind != this.EndKind)) { throw System.Data.Entity.ModelConfiguration.Resources.Error.ConflictingMultiplicities(this.NavigationProperty.Name, this.NavigationProperty.ReflectedType); } if ((navigationPropertyConfiguration.EndKind.HasValue && this.InverseEndKind.HasValue) && (navigationPropertyConfiguration.EndKind != this.InverseEndKind)) { throw System.Data.Entity.ModelConfiguration.Resources.Error.ConflictingMultiplicities(this.InverseNavigationProperty.Name, this.InverseNavigationProperty.ReflectedType); } 

In my case, the InverseNavigationProperty property is null, which causes the exception and WHEN EF tries to throw an exception ...

The main problem is that I had an inverse property relation defined, but no mapping was defined for it ...

However, it was not possible to verify which object was causing this problem ... These checks in this method should be wrapped using try catch to avoid this problem so that they can cause a more meaningful error for the end user ...

Thanks Greg

+4
source

Source: https://habr.com/ru/post/1384748/


All Articles