【问题标题】:NullReferenceException in DbContext.saveChanges()DbContext.saveChanges() 中的 NullReferenceException
【发布时间】:2013-06-12 18:05:28
【问题描述】:

在我第一次使用 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>

然后我尝试运行以下代码(来自 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 的调用是什么?

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

【问题讨论】:

  • aName 不是偶然为空吗?
  • 没有。我检查了。更重要的是,这会导致完全不同的异常。
  • 初始化 ContactID var contact = new DataContext.Contact { Name = aName , ContactID = 0 }; 看看问题是否依然存在
  • 仍然使用 Web 表单而不是 Razor,这很奇怪。
  • @Peuczyński 问题依然存在。

标签: c# asp.net entity-framework-5


【解决方案1】:

问题解决了。

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

可以理解,这与 Entity Framework 并没有很好地配合,导致上面的相当模糊的错误。删除aspx页面解决了问题。

【讨论】:

  • 我遇到了同样的问题,花了两个小时才弄清楚。谢谢。你的回答很有用。
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2021-08-01
    • 2017-12-03
    • 2014-07-01
    • 2010-11-05
    相关资源
    最近更新 更多