0

I've been using code first migrations for a long time now within my mvc application and it's always worked without any issues, today, however, I am getting an error of:

Object reference not set to an instance of an object.

This started after I added a new model which is:

using System;
using System.ComponentModel.DataAnnotations;

namespace MyProject.Models
{
    public class Tab
    {
        public int Id { get; set; }
        [Display(Name = "Tab Name")]
        public string TabName { get; set; }
        [Display(Name = "Name")]
        public string UserName { get; set; }
        [Display(Name = "Email")]
        public string UserEmail { get; set; }
        [Display(Name = "Created")]
        public DateTime? CreatedAt { get; set; }
        [Display(Name = "Filter String")]
        public string FilterString { get; set; }
    }
}

I added the database context in the same manner as previous tables

public DbSet<Tab> Tabs { get; set; }

I then added a migration because the table doesn't exist

Add-Migration SomeName

Which starts and then gives me the following error:

PM> Add-Migration Test
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Entity.Utilities.PropertyInfoExtensions.IsStatic(PropertyInfo property)
   at System.Data.Entity.Utilities.TypeExtensions.<GetInstanceProperties>b__22(PropertyInfo p)
   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
   at System.Data.Entity.Core.Metadata.Edm.MetadataPropertyCollection.ItemTypeInformation.GetItemProperties(Type clrType)
   at System.Data.Entity.Core.Metadata.Edm.MetadataPropertyCollection.<.cctor>b__0(Type clrType)
   at System.Data.Entity.Core.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0()
   at System.Data.Entity.Core.Common.Utils.Memoizer`2.Result.GetValue()
   at System.Data.Entity.Core.Common.Utils.Memoizer`2.Evaluate(TArg arg)
   at System.Data.Entity.Core.Metadata.Edm.MetadataPropertyCollection.GetSystemMetadataProperties(MetadataItem item)
   at System.Data.Entity.Core.Metadata.Edm.MetadataItem.GetMetadataProperties()
   at System.Data.Entity.Core.Metadata.Edm.MetadataItem.get_Annotations()
   at System.Data.Entity.Edm.EdmModelVisitor.VisitMetadataItem(MetadataItem item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.VisitMetadataItem(MetadataItem item)
   at System.Data.Entity.Edm.EdmModelVisitor.VisitEdmAssociationType(AssociationType item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.VisitEdmAssociationType(AssociationType item)
   at System.Data.Entity.Edm.EdmModelVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
   at System.Data.Entity.Edm.EdmModelVisitor.VisitAssociationTypes(IEnumerable`1 associationTypes)
   at System.Data.Entity.Edm.EdmModelVisitor.VisitEdmModel(EdmModel item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.VisitEdmModel(EdmModel item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.Dispatch()
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyConceptualModel(DbModel 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.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Object reference not set to an instance of an object.
PM> 

Nothing that I'm aware of has changed except for an application crash from a power cut earlier. It is probably unrelated but worth mentioning anyway.

What can I do to potentailly fix this?

4
  • 1
    Possibly related to stackoverflow.com/questions/9481784/…, though the stack is different. Try specifying the project, per the second answer there. Does it make any difference? Commented Jul 5, 2018 at 14:21
  • Thanks @Amy I had tried that before, but it doesn't change anything. Commented Jul 5, 2018 at 14:30
  • 1
    @Yanayaya Comment out half of the models properties and try again. Repeat until the error stops (excluding the necessary Id property). Does this make the error go away? Commented Jul 5, 2018 at 14:32
  • Thank's @Amy I gave that a go leaving only Id and the error persists. I may try removing the model and starting again. Commented Jul 5, 2018 at 14:35

2 Answers 2

2

Try adding [Key]

using System;
using System.ComponentModel.DataAnnotations;

namespace MyProject.Models
{
    public class Tab
    {
        [Key]
        public int Id { get; set; }
        [Display(Name = "Tab Name")]
        public string TabName { get; set; }
        [Display(Name = "Name")]
        public string UserName { get; set; }
        [Display(Name = "Email")]
        public string UserEmail { get; set; }
        [Display(Name = "Created")]
        public DateTime? CreatedAt { get; set; }
        [Display(Name = "Filter String")]
        public string FilterString { get; set; }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this answer, sadly, adding Key didn't change anything.
Mostly it happens because of missing the Key.
0

I'm not sure what has happened but because nothing "typical" was working, I had to repair visual studio. Now it works without a problem, at this stage, I have to blame the power cut and a potential corruption to the software.

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.