2

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)
Obi
  • 3,091
  • 4
  • 34
  • 56
  • did you also run **update-database**? – jbutler483 Aug 20 '14 at 12:21
  • 2
    Please add the code that throws this error – Ivo Coumans Aug 20 '14 at 12:21
  • @jbutler483 No didn't bother to because the add migration command failed – Obi Aug 20 '14 at 12:38
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Daniel Kelley Aug 20 '14 at 12:53
  • 1
    @DanielKelley I can't see the connection? – jbutler483 Aug 20 '14 at 12:54
  • @DanielKelley your suggestion has absolutely nothing to do with the question. The exception here isn't thrown by my code but by EF whose code I do not have access to and it is as a result of the absence of a property (albeit in my code), how EF uses that property to instantiate other classes internally is a different kettle of fish don't you think? – Obi Aug 20 '14 at 14:24
  • I love how I am running into the same "duplicate of What is a NullReferenceException and how do I fix it" comments on questions about EF Migrations. Do you guys even read these questions before firing your "this is a duplicate" load? – Douglas Gaskell Jul 06 '17 at 03:24

1 Answers1

3

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.

Obi
  • 3,091
  • 4
  • 34
  • 56
  • 2
    you should create a base class that has the Id and have all your entities inherit from it – SOfanatic Aug 20 '14 at 13:18
  • 2
    Which would be how I'd normally do it if I wasn't in a hurry to get the bleeding prototype out. Thanks – Obi Aug 20 '14 at 14:17