实体框架code第一对象引用不设置到对象的实例 [英] Entity Framework code first Object reference not set to an instance of an object

查看:386
本文介绍了实体框架code第一对象引用不设置到对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误,当我尝试首先使用EF code,

 对象引用未设置到对象的实例。

我得到这个,当我尝试运行例如像收取任何查询 db.Surveys.ToList()。我也曾尝试运行启用迁移命令,给出了同样的错误。
我的web应用程序是asp.net web窗体和我使用EFv6.1.1(最新的)。
我还添加了在我的web.config上下文名称的连接字符串。

我一直在使用的EntityFramework的年龄和从未有过这个问题,任何帮助是pciated AP $ P $

堆栈跟踪

  System.NullReferenceException:对象不设置到对象的实例。
   在System.Web.UI.ParseChildrenAttribute.GetHash code()
   在System.Collections.Generic.ObjectEqualityComparer`1.GetHash code(T OBJ)
   在System.Collections.Generic.HashSet`1.InternalGetHash code(T项)
   在System.Collections.Generic.HashSet`1.AddIfNot present(T值)
   在System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1等)
   在System.Collections.Generic.HashSet`1..ctor(IEnumerable`1收集,IEqualityComparer`1比较器)
   在System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(type类型)
   在System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.<>c__DisplayClass4.<GetAttributes>b__3(PropertyInfo PI)
   在System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKEY的关键,Func`2 valueFactory)
   在System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(的PropertyInfo的PropertyInfo)
   在System.Data.Entity.ModelConfiguration.Conventions.PropertyAttributeConfigurationConvention`1.<.ctor>b__0(ConventionTypeConfiguration EC)
   在System.Data.Entity.ModelConfiguration.Conventions.TypeConvention.ApplyCore(类型的MemberInfo,ModelConfiguration modelConfiguration)
   在System.Data.Entity.ModelConfiguration.Conventions.TypeConventionBase.Apply(类型的MemberInfo,ModelConfiguration modelConfiguration)
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type类型,ModelConfiguration modelConfiguration)
   在System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyModelConfiguration(类型类型,ModelConfiguration modelConfiguration)
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type类型,ModelConfiguration modelConfiguration)
   在System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(type类型)
   在System.Data.Entity.DbModelBuilder&LT;方式&gt;&c__DisplayClassd LT;地图类型&GT; b__7(类型类型)
   在System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   在System.Data.Entity.Utilities.IEnumerableExtensions.Each [T](IEnumerable`1 TS,Action`1动作)
   在System.Data.Entity.DbModelBuilder.MapTypes(EdmModel模型)
   在System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
   在System.Data.Entity.DbModelBuilder.Build(的DbConnection providerConnection)
   在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   在System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput输入)
   在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   在System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   在System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(的DbContext背景下,XmlWriter的作家)
   在System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter W)
   在System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1中WriteXML)
   在System.Data.Entity.Utilities.DbContextExtensions.GetModel(的DbContext上下文)
   在System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration配置的DbContext usersContext,DatabaseExistenceState existenceState,布尔calledByCreateDatabase)
   在System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration配置)
   在System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   在System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   在System.Data.Entity.Migrations.Design.ToolingFacade.Run(跑垒员亚军)
   在System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String语言,字符串rootNamespace)
   在System.Data.Entity.Migrations.EnableMigrationsCommand&LT;方式&gt;&c__DisplayClass2 LT; .ctor&GT; b__0()
   在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(动作命令)


解决方案

我找到了答案。

我错过了加入公众诠释标识{搞定;组; } 我的对象之一,显然实体框架不喜欢那场。

I get the following error when I try to use EF code first,

Object reference not set to an instance of an object.

I get this when I try to run ANY query on a collection like e.g. db.Surveys.ToList(). I have also tried to run the Enable Migrations command, gives the same error. My web app is asp.net web forms and I am using EFv6.1.1 (latest). I have also added a connection string with the context name in my web.config.

I have been using Entityframework for ages and never had this issue, any help is appreciated

Stacktrace

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.UI.ParseChildrenAttribute.GetHashCode()
   at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
   at System.Collections.Generic.HashSet`1.InternalGetHashCode(T item)
   at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value)
   at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
   at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer)
   at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(Type type)
   at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.<>c__DisplayClass4.<GetAttributes>b__3(PropertyInfo pi)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfo propertyInfo)
   at System.Data.Entity.ModelConfiguration.Conventions.PropertyAttributeConfigurationConvention`1.<.ctor>b__0(ConventionTypeConfiguration ec)
   at System.Data.Entity.ModelConfiguration.Conventions.TypeConvention.ApplyCore(Type memberInfo, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Conventions.TypeConventionBase.Apply(Type memberInfo, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type, ModelConfiguration modelConfiguration)
   at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
   at System.Data.Entity.DbModelBuilder.<>c__DisplayClassd.<MapTypes>b__7(Type type)
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   at System.Data.Entity.DbModelBuilder.MapTypes(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.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

解决方案

I found the answer.

I missed adding the public int Id {get; set; } field on one of my objects and apparently Entity Framework doesn't like that.

这篇关于实体框架code第一对象引用不设置到对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆