Entity Framework NullReferenceException when creating a model

I am currently working on a project as noob when it comes to WCF \ EF using entities \ tables created by another developer. I usually caught up with new things pretty quickly, and I have a small base for how to use the datacontext to do your work in the application. My only problem is that the context created by another developer does not seem to work, and I'm not sure if the problem is with the model, mappings, or table.

Here is the basic premise of the project. The developer first created a database, and then POCO objects with EF mappings for these objects into tables. He did this to simplify updating later with new columns / tables. When creating a model, the mappings are replaced by the OnModelCreating event instead of the EF mappings and the mappings are added to the model builder for each of the objects. Then the next thing that seems to happen after that is a NullReferenceException:

at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.ValidateConsistency(NavigationPropertyConfiguration navigationPropertyConfiguration) at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(EdmNavigationProperty navigationProperty, EdmModel model, EntityTypeConfiguration entityTypeConfiguration) at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociations(EdmEntityType entityType, EdmModel model) at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EdmEntityType entityType, EdmModel model) at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities(EdmModel model) at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 

My main problem is that I can not find any information that would lead me to the reason for this by searching, because I can not find other people who had this problem in this particular method. I would include code snippets from the \ POCOs \ use mappings, but I'm under the NDA for this project, and I don’t know to what extent this applies. My main problem is that someone can just tell me in which area should I look for the cause of this problem ... Setting up POCO, setting up mappings, or setting up tables? Any help would be greatly appreciated! I looked through all the code on my own several times, but I don’t seem to see anything, so some tricks will help. I will try to help with what I can.

+4
source share
1 answer

OK I am sending back so that if people encounter a problem inside this method later in their environment, they can understand where it is located.

Since it compiled and POCO looked great, I was sure that the error was related to mappings. I looked at the context in the project and commented on all the DbSet<x> declarations, as well as the display configurations for each of them. Then I walked one by one, uncommenting the dbset and configuration for the object. The context implementation will then be successful, and I would make the following object. I finally stayed with two related entities that have a one-to-one relationship, that when uncommenting, the final creation of the datacontext instance will fail.

The correspondence between the two elements was not correctly determined. This link has helped me Associate in EF Code First

This seems to be a mistake, because when checking the model, it could not navigate through the navigation properties, so I'm sure that the error can occur for several reasons, and not just for this reason, I just turned out to be the first to find it in this method.

Hope this helps!

+6
source

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


All Articles