Context.Entry()。GetDatabaseValues()方法不会复制实体的子节点 [英] Context.Entry().GetDatabaseValues() method doesn't copy entity's children
问题描述
Context.Entry()。GetDatabaseValues()
方法获取正在更新的对象的原始对象值。 .GetDatabaseValues()方法返回内部对象的空值的原始值 如下所示:
public partial class LabPayments
{
public partial class LabAccount
{
public long ID {get;组; }
public Nullable< long> MaterialID {get;组; }
public Nullable< long> LabID {get;组; }
public Nullable< double>数量{get;组; }
public Nullable< System.DateTime> MaterialDate {get;组; }
public Nullable< long> LabMaterialPriceID {get;组; }
public Nullable< decimal>价格{get;组; }
public virtual Lab Lab {get;组; }
public virtual LabMaterial LabMaterial {get;组; }
public virtual LabMaterialPrice LabMaterialPrice {get;组;
}
返回的虚拟对象值为null !!
从MSDN:
查询数据库的副本跟踪的实体的值目前存在于数据库中。
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();
}
请注意,返回的对象不是实体,也没有被跟踪上下文。返回的对象也没有与其他对象设置任何关系。
https://msdn.microsoft.com/en-us/data/jj592677.aspx
GetDatabaseValues
获取值仅用于跟踪的实体,而不是任何导航属性或集合属性,它们只是关系/关联的表示。这些虚拟属性是对相关实体对象的引用,不会以 GetDatabaseValues
返回。
I am working on a WPF project with Entity Framework. I used the Context.Entry().GetDatabaseValues()
method to get original object values for the object being updated. The .GetDatabaseValues() method returns original value with null values for inner objects
As the following :
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; }
}
The returned virtual object values are null !!
From 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
gets the values only for the tracked entity, not any navigation properties or collection properties, which are only representations of a relationship/association. Those virtual properties are references to related entity objects and won't be returned with GetDatabaseValues
.
这篇关于Context.Entry()。GetDatabaseValues()方法不会复制实体的子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!