Context.Entry()。GetDatabaseValues()方法不会复制实体的子节点 [英] Context.Entry().GetDatabaseValues() method doesn't copy entity's children

查看:187
本文介绍了Context.Entry()。GetDatabaseValues()方法不会复制实体的子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架处理WPF项目。我使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆