- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的简化数据库:
+----------+
|Products |
+----------+
|ProductID |
|ProdName |
|Desc |
|BrandID |
|CategoryID|
+----------+
+----------+
|Brands |
+----------+
|BrandID |
|BrandName |
|ImageID |
|Desc |
+----------+
+----------+
|Categories|
+----------+
|CategoryID|
|CategName |
|ImageID |
|Desc |
+----------+
+----------+
|Images |
+----------+
|ImageID |
|Path |
+----------+
+----------+
|ImageLinks|
+----------+
|ImageID |
|ProductID |
+----------+
(注:一个产品可以有多个图片,但一个品牌/类别最多可以有一个)
在 ASP.NET 中,
using (DBEntities db = new DBEntities())
{
Product product = db.Products.FirstOrDefault(p => p.ProductID == 1);
if (product != null) product.Desc = "any value";
db.SaveChanges(); //works
Image image = db.Images.FirstOrDefault(i => i.ImageID == 1);
if (image != null) image.Path = "any value";
db.SaveChanges(); //works
Brand brand = db.Brands.FirstOrDefault(b => b.BrandID == 1);
if (brand != null) brand.Desc = "the same value as the old description";
db.SaveChanges(); //works
Brand brand = db.Brands.FirstOrDefault(b => b.BrandID == 1);
if (brand != null) brand.Desc = "some new description";
db.SaveChanges(); //throws null reference exception
Category categ = db.Categories.FirstOrDefault(c => c.CategoryID == 1);
if (categ != null) categ.Desc = "the same value as the old description";
db.SaveChanges(); //works
Category categ = db.Categories.FirstOrDefault(c => c.CategoryID == 1);
if (categ != null) categ.Desc = "some new description";
db.SaveChanges(); //throws null reference exception
}
这很奇怪!
这是 SaveChanges() 抛出的 NullReferenceException 的堆栈跟踪
Object reference not set to an instance of an object.
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 VatechWebsite.Admin.Image_Upload.UploadBtn_Click(Object sender, EventArgs e) in c:\Users\Toshiba User\Desktop\vatech\VatechWebsite\VatechWebsite\Admin\Image_Upload.aspx.cs:line 109
PS:这是 Entity Framework 5
最佳答案
正如 divega 指出的那样,问题是我有一个与 EF 生成的实体同名的类(类别和品牌)。引用这篇文章NullReferenceException in DbContext.saveChanges()
关于asp.net - 更新记录属性后,EF 有时会在 SaveChanges() 上抛出 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19613708/
我一直想知道的一个问题是,我应该显式调用 context.SaveChanges() 还是让它在 Dispose() 下。 方法 1:自动保存 public virtual void Disp
我正在使用EF5和“数据库优先”方法在WPF中编写第一个MVVM应用程序。 我在MVVM中的模型是EF5为我生成的(我确实需要自定义T4模板以包括INotifyPropertyChanged)。 一切
我使用 Visual Studio 在 ASP.NET MVC 项目中处理本地数据库。 我在修改数据库时遇到问题。当我调用 SaveChanges() 时,我总是会收到此错误: The value c
我正在使用 Entity Framework 同时插入对象,如下所示。 context = new MyContext(); foreach (var x in lstX) { var abc
我在 ObjectSet 中添加数据并在 ObjectContext 上执行了 SaveChanges。但是新数据没有显示在 DataGrid 中! 代码: bsWork.DataSource = P
我正在使用 Oracle 数据库和 ASP.net MVC4 创建一个应用程序。即使在代码中看起来没有问题,调用 SaveChanges()方法导致此处显示的错误: 这张图片显示了内部异常和相关细节
我刚开始使用 Entity Framework ,我能够使用 linq 进行选择操作,但我在插入时遇到了问题。我试过这个示例,看看有什么问题: testEntities te = ne
假设我在 Controller 中调用了这样的东西: using (var context = new SqlContext()) { context.Items.Add(new Item("
我的 Asp.net mvc web 应用程序中有以下 Action 方法:- [HttpPost] [ValidateAntiForgeryToken] public ActionResult Cr
目标框架:netstandard2.0 Entity Framework 核心:2.2.6 我在 OnModelCreating 中有以下代码: protected override void OnM
这个问题在这里已经有了答案: DbContext SaveChanges Order of Statement Execution (1 个回答) 7年前关闭。 当上下文包含相关实体和 SaveCha
我有一个使用 Entity Framework 6.1、代码优先创建的 SQL-Azure 数据库。 我的“EmazeEvents”表中的“日期时间”字段是这样创建的: datetime = c.Da
查看人们编写的示例,我发现很多人使用 SaveChanges 而不是使用 SaveChangesWithRetries。我认为 SaveChangesWithRetries 是最好的选择,那么仅使用
RavenDB 遇到一个奇怪的问题 public ActionResult Save(RandomModel model) { //Do some stuff, validate model etc.
有没有办法保存单个跟踪对象的更改,而不是 ObjectStateManager 中的所有对象,我的意思是: ObjectContext.SaveChanges(Contact) 最佳答案 也许您可以创
查看人们编写的示例,我发现很多人使用 SaveChanges 而不是使用 SaveChangesWithRetries。我认为 SaveChangesWithRetries 是最好的选择,那么仅使用
我正在尝试编写一个生成多张发票的方法。这是一所大学,其中客户在名为 Enrollments 的类(class)中与导师一起注册。通过这种方法,我试图将导师客户的月费乘以他们的佣金百分比,因为导师从
为什么 DbContext ctx 在每次 SaveChanges() 执行后处理得更快? 第一个样本: var ctx = new DomainContext(); foreach (var ite
我一直在玩 Entity Core 类,例如 DbContext,在尝试保存对象时遇到以下错误: An error occurred while saving entities that do not
场景如下 - 不同的用户通过从网页的下拉列表中选择一个值来进行更改。下拉列表包含在 DataView 中或通过构建表格。如果用户 A 对第 1 行进行了更改,它会更新数据库并在重新绑定(bind)后显
我是一名优秀的程序员,十分优秀!