简体   繁体   中英

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:

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. After storing some Schemas with related Elements in database I load Schemas list (for example, var schemasList=context.Schemas.ToList()). After this in all instances of Schema in Elements property values is 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?

ie

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

note you will need

using System.Data.Entity 

to use include

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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