4

I am currently working on a project as a noob when it comes to WCF\EF, using entities\tables created by another developer. I am usually pretty quick at catching up on new things, and I have the small basics down on how to use the datacontext to do your work in the application. My only problem is that the context created by the other developer don't seem to be working, and I am unsure if the problem is with the model, the mappings, or the table.

Here is the basic premise of the project.. The developer created the database first, and then POCO objects with EF mappings for those objects, to the tables. He did this to ensure ease of upgrade later on with newer columns\tables. On the creation of the model, the mappings are substituted instead of EF's mappings by overriding the OnModelCreating event, and adding the mappings to the model builder for each of the objects. Then the next thing that seems to happen after this 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, I can't seem to find any information that would lead me to the cause of this by searching, as I can't seem to find other people that have had this problem in this particular method. I would include code snippets of the mappings\POCOs\usage, but I am under an NDA for this project, and I don't know to what extent that extends. My main problem is if someone can just tell me what area I should be looking at for the cause of this problem... POCO setup, Mappings setup, or Tables setup? Any help would be greatly appreciated! I have looked over all the code myself several times, but can't seem to spot anythings, so some focus would help. I will try to help with what I can.

1 Answer 1

6

OK. I am posting back here so if people encounter a problem within this method later in their environment, they can figure out where it lies.

Since it compiled fine, and the POCO's looked fine, I was pretty sure the error was with the mappings. I went throught the context in the project, and commented out all of the DbSet<x> declarations, as well as the mapping configurations for each one. I then went through one by one, uncommenting the dbset and configuration for an entity. The context instantiation would then succeed, and I would do the next entity. I was finally left with 2 related entities sharing a one-to-one relationship, that when uncommented, the final datacontext instantiation would fail.

The relationship mappings were not defined correctly between the two items. This link helped me out Associations in EF Code First

This appears to have errored because it could not navigate the navigationproperties when validating the model, so I am pretty sure the error can occur for more reasons than just this, I just happened to be the first to find it in this method.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.