添加迁移错误:对象引用未设置为对象的实例 [英] Add-Migration error: Object reference not set to an instance of an object

查看:168
本文介绍了添加迁移错误:对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是代码优先的概念,只是尝试向Models文件夹添加一个新类,希望在SQL Server数据库中创建一个新的表。我已经添加了

 公共覆盖属性诊断为DbSet(诊断)
/ pre>

在DBContext.vb中。



然后在包管理器控制台中,我进入了添加迁移诊断。但是下面的错误就出来了。为什么是这样?任何人都可以帮助我吗?


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


< blockquote>

包管理器控制台中的全部错误:

  System.NullReferenceException:对象引用不设置为对象的实例。 
在System.Web.UI.ParseChildrenAttribute.GetHashCode()
在System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
在System.Linq.Set`1.InternalGetHashCode (TElement value)
在System.Linq.Set`1.Find(TElement value,Boolean add)
在System.Linq.Enumerable。< ExceptIterator> d__72`1.MoveNext()
在System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
在System.Linq.Enumerable.ToList [TSource](IEnumerable`1源)
在System.Data。 Entity.ModelConfiguration.Utilities.AttributeProvider。< GetAttributes> b__3(PropertyInfo pi)
在System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key,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(Type memberInfo,ModelConfiguration modelConfiguration)
在System.Data.Entity.ModelConfiguration.Conventions.TypeConventionBase.Apply(类型memberInfo,ModelConfiguration modelConfiguration)
在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type,ModelConfiguration modelConfiguration)
在System.Data.Entity.ModelConfiguration.Conventions.Convention.ApplyModelConfiguration(Type type,ModelConfiguration modelConfiguration)
在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModelConfiguration(Type type,ModelConfiguration modelConfiguration)
在System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapComplexType(Type type,Boolean discoverNested)
在System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapPrimitiveOrComplexOrEnumProperty(PropertyInfo propertyInfo,F在System.Data.Entity.ModelConfiguration中的
, .Mappers.TypeMapper。  c__DisplayClass14。< MapEntityType> b__e(PropertyMapper m,PropertyInfo p)
在System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapStructuralElements [TStructuralTypeConfiguration](Type type,ICollection 1个注释,Action`2 propertyMappingAction,Func`1 structuralTypeConfiguration)
在System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(类型类型)
在System.Data.Entity.ModelConfiguration.Mappers。 NavigationPropertyMapper.Map(PropertyInfo propertyInfo,EntityType entityType,Func`1 entityTypeConfiguration)
在System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
在System.Da ta.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 existsState,Boolean 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()
在系统。 AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner转轮)
在系统。 Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName,String language,String rootNamespace,Boolean ignoreChanges)
在System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name,Boolean force,Boole一个ignoreChanges)
在System.Data.Entity.Migrations.AddMigrationCommand。  c__DisplayClass2。< .ctor> b__0()
在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(动作命令)
对象引用未设置为对象的实例。

我不知道这是否相关,但在我的web.config中,当我第一次创建我的项目时已经预先设置的默认连接。当我第一次执行Enable-Migrations时,我必须指定它是什么连接。我删除了该行,但仍然不起作用。

解决方案

以前,当我做了添加迁移,它没有工作,因为我只是创建一个类,并添加了相应的字符串,整数等作为这样的变量:

 公共属性id as String 

现在我突然发现我不得不对每个变量进行更详细的描述,如下所示:

 < Key> 
< StringLength(20)>
公共属性id as String

现在它的工作=')


I'm using code-first concept and just tried adding a new class to the Models folder in hope to create a new table in SQL server database. i've added

Public Overridable Property Diagnosis As DbSet(Of Diagnosis)

in the DBContext.vb.

Then in the package manager console, i entered Add-Migration Diagnosis. But below error comes out. Why is this? Can anyone help me out please?

Object reference not set to an instance of an object.

Full error in Package Manager Console:

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.Linq.Set`1.InternalGetHashCode(TElement value)
   at System.Linq.Set`1.Find(TElement value, Boolean add)
   at System.Linq.Enumerable.<ExceptIterator>d__72`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.<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.MapComplexType(Type type, Boolean discoverNested)
   at System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapPrimitiveOrComplexOrEnumProperty(PropertyInfo propertyInfo, Func`1 structuralTypeConfiguration, Boolean discoverComplexTypes)
   at System.Data.Entity.ModelConfiguration.Mappers.PropertyMapper.MapIfNotNavigationProperty(PropertyInfo propertyInfo, EntityType entityType, Func`1 entityTypeConfiguration)
   at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.<>c__DisplayClass14.<MapEntityType>b__e(PropertyMapper m, PropertyInfo p)
   at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapStructuralElements[TStructuralTypeConfiguration](Type type, ICollection`1 annotations, Action`2 propertyMappingAction, Func`1 structuralTypeConfiguration)
   at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
   at System.Data.Entity.ModelConfiguration.Mappers.NavigationPropertyMapper.Map(PropertyInfo propertyInfo, EntityType entityType, Func`1 entityTypeConfiguration)
   at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.MapEntityType(Type type)
   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.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.

I don't know if this is relevant or not, but in my web.config, there is a Default Connection that is already pre-set when i first created my project. When i first did Enable-Migrations i had to specify which connection it was. I've removed that line but it still doesn't work.

解决方案

Previously when i did the add-migration, it didn't work because i simply created a class and added declaration of the respective strings, integer, etc. as variables like this:

Public Property id As String

Now it suddenly popped into my mind that i had to give a more elaborate description on each of the variables such as below:

<Key>
<StringLength(20)>
Public Property id As String

Now it works =')

这篇关于添加迁移错误:对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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