DrAlligieri
DrAlligieri

Reputation: 211

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 (e.g. saving may cause "Entity tracking by multiple context" error). Thank for any help with this problem.

Upvotes: 0

Views: 853

Answers (1)

undefined
undefined

Reputation: 34238

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

Upvotes: 2

Related Questions