关于调用的SaveChanges奇怪DbEntityValidationException [英] Strange DbEntityValidationException on calling SaveChanges
问题描述
我忙于开发利用MVC4一个ASP.net Web应用程序,并试图挽救一些变化分贝时,就遇到了这个奇怪的错误。此代码是在我的控制器之一。下面的代码会导致当_db.Save()被调用DbEntityValidationException这反过来调用调用SaveChanges()。我是用的EntityFramework V5工作
文档文件= _db.Documents.SingleOrDefault(X => x.ID == DOC 。ID);
如果(文件!= NULL)
{
document.Location = idPath;
_db.Save();
}
异常消息:
但是:当我使用下面的代码我没有得到任何异常,路径获取保存成功分贝。
文档文件= _db.Documents .FirstOrDefault(X => x.ID == doc.ID);
如果(文件!= NULL)
{
//为所需的SaveChanges工作
变种X = document.Type;
document.Location = idPath;
_db.Save();
}
为什么会出现这种情况?是,可能是因为我的Documents集合的类型是List?需要注意的是我发现的错误是由Type属性造成的。
下面是我的文档类的结构:
[表(文档)]
公共类文件
{
[关键]
[DatabaseGenerated(DatabaseGeneratedOption.Identity )
公共虚拟INT ID {搞定;组; }
[必填]
公共虚拟字符串名称{;组; }
公共虚拟字符串的位置{搞定;组; }
[必填]
公共虚拟DocumentType类型{搞定;组; }
[NotMapped]
公共虚拟HttpPostedFileBase文件{搞定;组; }
}
我认为这个问题是。因为延迟加载
在事实上,通过调用该行:
文档文件= _db.Documents.SingleOrDefault(X => x.ID == doc.ID);
您只得到文件
实体,其导航性能保持空...! (设置一个破发点,并期待)
然而,当你调用这个行:
VAR X = document.Type;
您强制EF查询数据库来获取键入
导航属性到内存并将其连接到的DbContext。事实上,这是一个正常的行为,延迟加载! - 除非真正需要没有得到任何东西。
所以,你看,这当然不是一个奇怪的错误!它只是懒加载...
的副作用
I am busy developing a ASP.net web application using MVC4 and ran into this strange error when trying to save some changes to the db. This code is in one of my controllers. The below code causes a DbEntityValidationException when _db.Save() is called which in turn calls SaveChanges(). I am working with EntityFramework V5.
Document document = _db.Documents.SingleOrDefault(x => x.ID == doc.ID);
if (document != null)
{
document.Location = idPath;
_db.Save();
}
The exception message:
But: When I use the following code I get no exception and the path gets saved to the db successfully.
Document document = _db.Documents.FirstOrDefault(x => x.ID == doc.ID);
if (document != null)
{
// Needed for SaveChanges to work
var x = document.Type;
document.Location = idPath;
_db.Save();
}
Why would this happen? Is it maybe because my Documents collection is of type List? Note that I have found that the error is caused by the Type property.
Below is the structure of my Document class:
[Table("Document")]
public class Document
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int ID { get; set; }
[Required]
public virtual string Name { get; set; }
public virtual string Location { get; set; }
[Required]
public virtual DocumentType Type { get; set; }
[NotMapped]
public virtual HttpPostedFileBase File{ get; set; }
}
I think the problem is because of Lazy Loading
.
In fact, by calling this line:
Document document = _db.Documents.SingleOrDefault(x => x.ID == doc.ID);
You get only the scalar properties of the Document
entity and its navigation properties remain null...! (Set a break point and look).
However, when you call this line:
var x = document.Type;
You force the EF to query the database to fetch the Type
navigation property into the memory and attach it to the dbcontext. Indeed it's a normal behavior, Lazy loading! - don't get anything unless that's really needed.
So, as you see, It's of course not a strange error! it's just a side effect of lazy loading...
这篇关于关于调用的SaveChanges奇怪DbEntityValidationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!