DbContext.saveChanges()中的NullReferenceException

以我的第一个babysteps与Entity Framework 5.0,我遇到了我创建的第一个实体的exception。

请注意,之后创建的每个表都可以正常工作。 另外,请注意我已经采取了重新生成数据库和/或重新启动Visual Studio IDE的常规步骤。

使用Model-First,我创建了一个名为Contacts的简单表,定义为

         

然后我尝试运行以下代码(来自ASP.NET页面的Page_Load)

  var contact = new DataContext.Contact { Name = aName }; context.Contacts.Add(contact); context.SaveChanges(); 

(使用aName!= null)

例外:

 System.NullReferenceException was unhandled by user code HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=System.Web StackTrace: 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.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection) at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(Type type) at System.Data.Entity.ModelConfiguration.Utilities.AttributeProvider.GetAttributes(PropertyInfo propertyInfo) at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildPropertyValidator(PropertyInfo clrProperty) at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildValidatorsForProperties(IEnumerable`1 clrProperties, IEnumerable`1 edmProperties, IEnumerable`1 navigationProperties) at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildTypeValidator[T](Type clrType, IEnumerable`1 edmProperties, IEnumerable`1 navigationProperties, Func`3 validatorFactoryFunc) at System.Data.Entity.Internal.Validation.EntityValidatorBuilder.BuildEntityValidator(InternalEntityEntry entityEntry) at System.Data.Entity.Internal.Validation.ValidationProvider.GetEntityValidator(InternalEntityEntry entityEntry) at System.Data.Entity.Internal.InternalEntityEntry.GetValidationResult(IDictionary`2 items) at System.Data.Entity.DbContext.ValidateEntity(DbEntityEntry entityEntry, IDictionary`2 items) at System.Data.Entity.DbContext.GetValidationErrors() at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at Contactisch._Default.AddContact(String aName) in c:\Projects\Contactisch\Contactisch\Contactisch\Default.aspx.cs:line 32 at Contactisch._Default.Page_Load(Object sender, EventArgs e) in c:\Projects\Contactisch\Contactisch\Contactisch\Default.aspx.cs:line 14 at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException: 

有人可以解释这个例外的原因吗? 特别是,那对ParseChildrenAttribute.GetHashCode的调用是什么?

我确实发现有人遇到同样的问题,但没有给出令人满意的解释。

在从数据库生成edmx和EF类之后创建与数据库中的表同名的ASP.NET网页时会发生这种情况Visual Studio会将它们放在项目的默认命名空间中,从而导致与生成的项冲突网页的部分类.aspx.cs文件

您不需要删除或重命名网页的.aspx文件只需更改后面的页面代码(myPage.aspx.cs)中声明的部分公共类的名称,并像这样适当地调整页面的Inherits属性。

 <%@Page Title="MyPage" ... Inherts="namespace.newClassName" > 

或者,您始终可以在不同的命名空间下声明您的网页。

问题解决了。

原因有点傻。 我使用VS Web Express中的默认ASP.NET Web Forms Application项目来执行我的测试。 此项目包含一个名为Contact.aspx的Web表单, 因此它已在与Contact实体相同的命名空间中包含一个部分类Contact

可以理解的是,这与entity framework不相符,导致上面相当模糊的错误。 删除aspx页面解决了这个问题。