我正在使用 Entity Framework 开发 WPF 项目。我使用 Context.Entry().GetDatabaseValues() 方法来获取正在更新的对象的原始对象值。 .GetDatabaseValues() 方法返回原始值,内部对象为空值
如下:
public partial class LabPayments
{
public partial class LabAccount
{
public long ID { get; set; }
public Nullable<long> MaterialID { get; set; }
public Nullable<long> LabID { get; set; }
public Nullable<double> Quantity { get; set; }
public Nullable<System.DateTime> MaterialDate { get; set; }
public Nullable<long> LabMaterialPriceID { get; set; }
public Nullable<decimal> Price { get; set; }
public virtual Lab Lab { get; set; }
public virtual LabMaterial LabMaterial { get; set; }
public virtual LabMaterialPrice LabMaterialPrice { get; set; }
}
返回的虚拟对象值为空!!
最佳答案
来自 MSDN:
Queries the database for copies of the values of the tracked entity as they currently exist in the database.
https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.getdatabasevalues(v=vs.113).aspx
using (var context = new BloggingContext()) { var blog = context.Blogs.Find(1); var clonedBlog = context.Entry(blog).GetDatabaseValues().ToObject(); }
Note that the object returned is not the entity and is not being tracked by the context. The returned object also does not have any relationships set to other objects.
https://msdn.microsoft.com/en-us/data/jj592677.aspx
GetDatabaseValues
仅获取跟踪实体的值,而不是任何导航属性或集合属性,它们只是关系/关联的表示。这些虚拟属性是对相关实体对象的引用,不会通过 GetDatabaseValues
返回。
关于c# - Context.Entry().GetDatabaseValues() 方法不会复制实体的子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32831168/