Asp.net 更新记录属性后,EF在SaveChanges()上引发NullReferenceException。。有时

Asp.net 更新记录属性后,EF在SaveChanges()上引发NullReferenceException。。有时,asp.net,entity-framework,webforms,entity-framework-5,nullreferenceexception,Asp.net,Entity Framework,Webforms,Entity Framework 5,Nullreferenceexception,我的简化数据库: +----------+ |Products | +----------+ |ProductID | |ProdName | |Desc | |BrandID | |CategoryID| +----------+ +----------+ |Brands | +----------+ |BrandID | |BrandName | |ImageID |

我的简化数据库:

   +----------+
   |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的堆栈跟踪


PS:这是实体框架5

我认为您分配实体模型对象类的方式不正确。 将以下更改应用于代码中的所有实体对象,然后重试:

Product Product=db.Products.FirstOrDefault(p=>p.ProductID==1);/*对于产品*/

在类别、形象和品牌方面也是如此


最好的。

正如divega指出的,问题是我有一个与EF生成的实体同名的类(包括类别和品牌)。参考这篇文章

你可以在该方法结束时调用
SaveChanges
一次……我也试过了,但没有成功……堆栈跟踪显示异常来自SaveChanges内部发生的验证。默认情况下,此验证基于DataAnnotation属性,显然在您的情况下,与System.Web中的某些属性存在一些交互。可能是实体类或其属性中的某些属性代码中存在错误?在最坏的情况下,您可以在保存时禁用验证,但看看您的实体类型会很有趣。。禁用saves上的验证是可行的,但我不确定实体类中可能存在什么样的错误。。我首先使用数据库并依靠EF生成此代码。我所有的非ID列都可以为空,所以我不确定会有什么问题。这确实很有趣。我以前没有看到过这种情况,但我刚才进行了搜索,发现了以前的线程:。你能确认这解决了问题吗?很抱歉,这是我在真实案例中所做的,但我复制了错误的代码。我会更新我的问题
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