DbContext.saveChanges()中的NullReferenceException

Pau*_*Jan 11 c# asp.net entity-framework-5

以我的第一个babysteps与Entity Framework 5.0,我遇到了我创建的第一个实体的异常.

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

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

  <EntityType Name="Contacts">
    <Key>
      <PropertyRef Name="ContactID" />
    </Key>
    <Property Name="ContactID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
    <Property Name="Name" Type="nvarchar(max)" Nullable="false" />
  </EntityType>
Run Code Online (Sandbox Code Playgroud)

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

            var contact = new DataContext.Contact { Name = aName };

            context.Contacts.Add(contact);
            context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

(使用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: 
Run Code Online (Sandbox Code Playgroud)

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

我确实发现有人运行到同一个问题在这里,但没有给出令人满意的解释.

Sir*_*rhc 6

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

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

<%@Page Title="MyPage" ... Inherts="namespace.newClassName" >
Run Code Online (Sandbox Code Playgroud)

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


Pau*_*Jan 5

问题解决了.

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

可以理解的是,这与实体框架不相符,导致上面相当模糊的错误.删除aspx页面解决了这个问题.

  • 我有同样的问题,失去了两个小时才弄明白.谢谢.你的答案非常有用. (3认同)