简体   繁体   English

EF 4.3和CodeFirst:一对多导航属性在没有实体代理的情况下加载为空值

[英]EF 4.3 & CodeFirst: One-to-many navigation properties loads as nulls without entity proxy

Problem is next - i have 2 entities with one-to-many relationships: 接下来是问题 - 我有2个具有一对多关系的实体:

public class Schema
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid SchemaId { get; set; }

    public string Name { get; set; }

    public string Content { get; set; }

    public string ElementName { get; set; }

    public List<Element> Elements { get; set; }
}

public class Element
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid ElementId { get; set; }

    public Guid SchemaId { get; set; }

    public string Content { get; set; }

    public Schema InSchema { get; set; }
}

and project referenced to EntityFramework v4.3 package. 和项目引用EntityFramework v4.3包。 After storing some Schemas with related Elements in database I load Schemas list (for example, var schemasList=context.Schemas.ToList()). 在数据库中存储了一些带有相关元素的Schema后,我加载了Schemas列表(例如,var schemasList = context.Schemas.ToList())。 After this in all instances of Schema in Elements property values is null. 在此之后,在Elements中的Schema的所有实例中,属性值为null。 Now i solve this problem with adding dynamic proxy for entities, but this have some bad impact in numerous dev scenarious (eg saving may cause "Entity tracking by multiple context" error). 现在我通过为实体添加动态代理来解决这个问题,但是这在许多开发方面有一些不良影响(例如,保存可能导致“多个上下文的实体跟踪”错误)。 Thank for any help with this problem. 感谢您对此问题的任何帮助。

Have you used .Include in your query? 你在查询中使用过.Include吗?

ie

from s in Schema.Include(sc=>sc.Elements)
select s;

note you will need 请注意,你需要

using System.Data.Entity 

to use include 使用包括

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM