Reputation: 2184
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?
Upvotes: 0
Views: 183
Reputation: 2184
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.
Upvotes: 0
Reputation: 541
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; }
}
}
Upvotes: 2