gpt4 book ai didi

asp.net - 更新记录属性后,EF 有时会在 SaveChanges() 上抛出 NullReferenceException

转载 作者:行者123 更新时间:2023-12-02 17:00:15 25 4
gpt4 key购买 nike

我的简化数据库:

   +----------+
|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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com