asp.net – EF更新记录属性后在SaveChanges()上抛出NullReferenc
发布时间:2020-12-16 09:39:47 所属栏目:asp.Net 来源:网络整理
导读:参见英文答案 NullReferenceException in DbContext.saveChanges()????????????????????????????????????2个 我的简化数据库: +----------+ |Products | +----------+ |ProductID | |ProdName | |Desc | |BrandID | |CategoryID| +----------+ +----------+
参见英文答案 >
NullReferenceException in DbContext.saveChanges()????????????????????????????????????2个
我的简化数据库: +----------+ |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 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:UsersToshiba UserDesktopvatechVatechWebsiteVatechWebsiteAdminImage_Upload.aspx.cs:line 109 PS:这是实体框架5 解决方法
正如divega指出的那样,问题在于我有一个与EF生成的实体同名的类(包括类别和品牌).请参阅此文章
NullReferenceException in DbContext.saveChanges()
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-routing – ASP.NET Web Api路由(IIS vs Self
- asp.net – 如何从RouteData获取路由名称?
- asp.net mvc partialview @ Ajax.ActionLink不起作用
- asp.net-mvc – 如何从HttpContext访问ViewData? (ASP.Net
- asp.net web api定义的b/s调用方法一例
- 清除ASP.NET临时程序集而不重新启动IIS
- Asp.net下使用Jquery Ajax传送和接收DataTable的代码
- asp.net-mvc – !ClrStack -a调用ASP.NET MVC应用程序显示
- asp.net-mvc – 使用razor引擎在mvc4中重写url
- asp.net-mvc – IIS 7与IIS Express的路由错误,生成HTTP 40
推荐文章
站长推荐
- asp.net – 在处理期间存储用户名/密码
- ASP.NET OnClientClick =“return false;”不起作
- 我可以使用其他参数增加MVC Controller Action的
- asp.net-mvc – DropDownListFor在编辑视图上不重
- asp.net – 保护.net Web服务URL
- asp.net-mvc – ASP.NET MVC和Visual Studio 201
- 在ASP.NET MVC 3应用程序中编辑并继续
- ASP.NET中验证控件的使用
- asp.net – OWIN AuthorizeEndpoint,其redirect_
- asp.net – 使用jQuery调用MVC中的action方法,参
热点阅读