- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在使用 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/
我已经开始研究使用 Identity 框架的现有 ASP.NET Core 项目。该应用程序使用单个数据库。出于某种原因,应用程序使用两个独立的数据库上下文 - 一个派生自 IdentityDbCon
我刚刚下载了EntityFramework.dll v4.3。我发现了许多比较 DbContext 与 ObjectContext 的问题。但其中大部分是 2010 年或 2011 年初的。 我想阅读
我想做什么 public void SomeTestMethod(){ var person = this.PersonManager.Get(new Guid("someguid"));
在我的应用程序中,我刚刚将 EntityFramework 更新到版本 6.0.2,现在我遇到了一些错误,这是我在更新我的应用程序之前没有发现的。 我有一个从 IdentityDbContext 类继
我正在处理一个大型项目,每个 DbContext 下有 50 多个 DbContext 和 100 多个 DbSet。 每个 DbContext 都由一个单独的团队处理,他们根据他们的要求/更改添加/
我是 WPF 的初学者。我想知道 dbcontext.Add 和 dbcontext.AddObject 之间有什么区别。 private void AddButton_Click(object se
我正在使用 MVC .Net。通常,每当我需要查询数据库时,我总是使用类似下面的方法来创建一个实例: using (EntitiesContext ctx = new EntitiesContext(
我在 HomeController 中有一个方法,我试图通过 URL 访问它,如下所示: http://localhost/web/home/GetSmth 第一次工作,但刷新页面后,我收到此错误:
在我的 Controller 中,我有这个 private readonly DbContext _context; public CountryController(DbContext contex
我正在寻找一种回滚实体更改的方法。我遇到了this answer它显示了如何设置实体状态,但我想知道如果我只是处理我的 dbContext 实例而不调用 dbContext.SaveChanges()
在我的项目中,我使用entity framework 7 和asp.net mvc 6\asp.net 5。我想为自己的模型创建 CRUD 我怎样才能做得更好: 使用 Controller 中的 db
我正在使用 Asp.Net Core 2.1 开发 Web 应用程序。在我使用脚手架添加新身份后,它为我生成了这些代码: IdentityStartup.cs 中生成的代码 [assembly:Hos
一旦我解决了one issue与 IBM.EntityFrameworkCore ,另一个出现了。对于 DB2 和他们的 .NET 团队来说,一切都是那么艰难和痛苦...... 问题:我在同一个 VS
我正在尝试创建一个播种用户和角色数据的类。 我的播种数据类(class)采用RoleManager构造函数参数 public class IdentityDataSeeder { private
我正在使用 .NET Core 2.1 构建 Web API。这将作为 Azure Web 应用程序托管。我想将数据库连接字符串保留在 Azure Key Vault 中。这是我放入 Startup.
当使用像 MySQL 这样的网络数据库时,DbContext 应该是短暂的,但是根据 https://www.entityframeworktutorial.net/EntityFramework4.
我有一个直接调用的 Controller 操作,但抛出了这个错误: The operation cannot be completed because the DbContext has been d
我在 Visual Studio 中使用默认的 ASP.Net MVC 模板。我正在使用在模板中为我创建的 ASP.Net 身份代码。我希望我使用的 DBContext 了解 ApplicationU
我有一个软件产品,它的数据库是在 SQLServer 上创建的,表名和列名是由开发团队定义的,然后使用 Database First 方法将模型导入 Visual Studio,现在我们正在为其他公司
我正在使用 EFCore 加载“用户”实体和用户制作的相关“订单”。 我有一个构造函数(来自真实代码的简化示例),它使用 id=1 加载用户并实现一个命令来更新 LoadedUser 实体中的更改。但
我是一名优秀的程序员,十分优秀!