简体   繁体   English

模型创建期间的实体框架NullReferenceException

[英]Entity Framework NullReferenceException during model creation

I am currently working on a project as a noob when it comes to WCF\\EF, using entities\\tables created by another developer. 我正在使用另一个开发人员创建的实体\\表来处理WCF \\ EF时作为菜鸟的项目。 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. 我通常很快就能抓住新事物,而且我对如何使用datacontext在应用程序中完成工作有一些小的基础知识。 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. 这是项目的基本前提。开发人员首先创建数据库,然后使用EF映射为这些对象创建POCO对象到表中。 He did this to ensure ease of upgrade later on with newer columns\\tables. 他这样做是为了确保以后使用较新的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. 在创建模型时,通过覆盖OnModelCreating事件并将映射添加到每个对象的模型构建器来替换映射而不是EF的映射。 Then the next thing that seems to happen after this is a NullReferenceException: 然后,接下来似乎发生的事情是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. 我将包含映射\\ POCOs \\用法的代码片段,但我在这个项目的NDA下,我不知道扩展到什么程度。 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? 我的主要问题是,如果有人可以告诉我应该在哪个方面查找此问题的原因... POCO设置,映射设置或表设置? 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. 我会尽力帮助。

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. 由于它编译得很好,POCO看起来很好,我很确定错误与映射有关。 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. 我浏览了项目中的上下文,并注释掉了所有DbSet<x>声明,以及每个声明的映射配置。 I then went through one by one, uncommenting the dbset and configuration for an entity. 然后我逐个完成,取消注释实体的dbset和配置。 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. 我最终留下了两个相关的实体,它们共享一对一的关系,当取消注释时,最终的datacontext实例化将失败。

The relationship mappings were not defined correctly between the two items. 两个项目之间没有正确定义关系映射。 This link helped me out Associations in EF Code First 这个链接帮助我解决了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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM