c# - Context.Entry().GetDatabaseValues() 方法不会复制实体的子级

标签 c# entity-framework

我正在使用 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/

相关文章:

asp.net-mvc - AspNet.Identity 自定义用户和自定义角色应该很简单;我错过了什么?

c# - Entity Framework Core 导航属性不会保持为空

c# - 我需要添加什么路由来映射到 API Controller ?

c# - 如何在 C# 中确定多播数据包的源 IP?

c# - 测试项目在它正在测试的项目中找不到对象

c# - 具有AutoMapper的体系结构?

c# - 异常处理 : how granular would you go when it comes to argument validation?

jquery - ASP.NET MVC : How to 'model bind' a non html-helper element

c# - 如何检查我的 SQL Server Express 数据库是否超过 10 GB 的大小限制?

c# - 如何使用 Entity Framework 4 访问存储过程的 'Results'、 'Messages' 和 'Return Value'?