c# - DbContext.saveChanges() 中的 NullReferenceException

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

在使用 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)

异常(exception):

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 , 但没有给出令人满意的解释。

最佳答案

问题解决了。

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

可以理解,这在 Entity Framework 中表现不佳,导致了上面相当模糊的错误。删除 aspx 页面解决了问题。

关于c# - DbContext.saveChanges() 中的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17136455/

相关文章:

c# - 代码优先 : Mapping entities to existing database tables

c# - Linq 查询在几分之一秒内运行,但 .ToList() 需要 3.5 秒

c# - Unity Camera Size 网络播放器

c# - 在 Raspberry PI 上管理 Wifi 连接

asp.net - 在我的代码隐藏类中,如何检索授权角色?

c# - Gridview在 View 状态中使用自定义对象插入删除

javascript - 在服务器端捕获所有 JavaScript 客户端错误

C# 递增 ToString

c# - asp.net 从数据库中读取图片并输出

entity-framework - EF Power Tools - 序列不包含匹配元素