C#学习教程:DbContext.saveChanges()中的NullReferenceException请注意,之后创建的每个表都可以正常工作。另外请注意,我已经采取了重建数据库和/或重新启动VisualStudioIDE的常规步骤。使用Model-First,我创建了一个名为Contacts的简单表,定义为然后我尝试运行以下代码(来自ASP.NET页面的Page_Load)varcontact=newDataContext.Contact{Name=aName};上下文。联系人。增加联系人);context.SaveChanges();(aName!=null)异常:System.NullReferenceException未被用户代码处理HResult=-2147467261Message=Objectreferencenotsettoaninstanceofanobject.Source=System.WebStackTrace:在System.Web.UI.ParseChildrenAttribute.GetHashCode()在System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(Tobj)在System.Collections.Generic.HashSet`1.InternalGetHashCode(Titem)在System.Collections.Generic.HashSet`1.AddIfNotPresent(T值)在System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1其他)在System.Collections.Generic.HashSet`1..ctor(IEnumerable`1集合,IEqualityComparer`1比较器)在System.Collections.Generic.HashSet`1..ctor(IEnumerable`1集合)在System.Data.Entity.ModelConfiguration.Utilities.AttributeProSystem.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildPropertyValidator(PropertyInfoclrProperty)在System.Data.Entity的System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfopropertyInfo)处的vider.GetAttributes(类型类型)。Internal.Validation.EntityValidatorBuilder.BuildValidatorsForProperties(IEnumerable`1clrProperties,IEnumerable`1edmProperties,IEnumerable`1navigationProperties)在System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildTypeValidator[T](类型clrType,IEnumerable`1edmProperties,IEnumerable`1navigationProperties,Func`3validatorFactoryFunc)在System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildEntityValidator(InternalEntityEntryentityEntry)在System.Data.Entity.Internal.Validation.ValidationProvider.GetEntityValidator(InternalEntityEntryentityEntry)在System.Data。Entity.Internal.InternalEntityEntry.GetValidationResult(IDictionary`2项)在System.Data.Entity.DbContext.ValidateEntity(DbEntityEntryentityEntry,IDictionary`2项)在System.Data.Entity.DbContext.GetValidationErrors()在System.Data.Entity.Internal.InternalContext.SaveChanges()在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()在System.Data.Entity.DbContext.SaveChanges()在Contactisch._Default.AddContact(StringaName)在c:ProjectsContactischContactischContactischDefault.aspx.cs:第32行在Contactisch._Default.Page_Load(对象发送者,EventArgse)在c:ProjectsContactischContactischContactischDefault.aspx.cs:line14atSystem.Web.Util.CalliEventHandlerDelegateProxy.Callback(Objectsender,EventArgse)atSystem.Web.UI.Control.OnLoad(EventArgse)atSystem.Web.UI.Control.LoadRecursive()atSystem.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)InnerException:有人可以解释这个异常的原因吗?特别是,对ParseChildrenAttribute.GetHashCode的调用是什么?我确实找到了有同样问题的人问题,但没有给出令人满意的解释。当您在从数据库生成edmx和EF类后创建与数据库中的表同名的ASP.NET网页时,会发生这种情况。VisualStudio将它们放在项目的默认命名空间中。.aspx.cs文件你不需要删除或重命名网页的.aspx文件只需更改页面后面的代码(myPage.aspx.cs)中声明的部分公共类,并适当调整页面的Inherits属性,如这。或者,您始终可以在不同的命名空间下声明您的页面。问题已经解决了。理由有点傻。我在VSWebExpress中使用默认的ASP.NETWeb窗体应用程序项目来执行我的测试。该项目包含一个名为Contact.aspx的Web窗体,因此它已在与Contact实体相同的命名空间中包含一个分部类Contact。可以理解,这与实体框架不一致,导致上面相当模糊的错误。删除aspx页面解决了问题。以上就是C#学习教程的全部内容:DbContext.saveChanges()中的NullReferenceException。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处:
