3

First some brief background: I have an existing ASP.NET MVC 1 application using Entity Framework v1 which works fairly well, though because there are getting on to 40 tables the .edmx is getting unwieldy and prone to corruptions with the Visual Studio 2008 designer. What I want to do is to see if it's feasible to migrate the DAL to use EF4 and Code-First.

Initially I'm trying to model simple parent/child relationships but not getting very far. I have 2 tables Client and Address which correspond to the following POCO classes:

public class Client
{
    public int ClientId { get; set; }
    public string Name { get; set; }
    public Address HomeAddress { get; set; }
    public Address WorkAddress { get; set; }
    // More properties here
}

public class Address
{
    public int AddressId { get; set; }
    public string NameOrNumber { get; set; }
    public string Line1 { get; set; }
    // More properties here
}

In addition I have a DbContext class in which I define the relationships using the fluent api:

public class AppContext : DbContext
{
    public SlobContext() : base()
    {
        this.ObjectContext.ContextOptions.LazyLoadingEnabled = true;
    }

    public DbSet<Client> Clients
    {
        get;
        set;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>().MapSingleType().ToTable("Address");
        modelBuilder.Entity<Client>().HasKey(c => c.ClientID);
        modelBuilder.Entity<Client>().HasRequired<Address>(c => c.HomeAddress);
        modelBuilder.Entity<Client>().HasRequired<Address>(c => c.WorkAddress);

        modelBuilder.Entity<Client>()
            .MapSingleType(c => new
            {
                Name = c.Name,
                ClientID = c.ClientID,
                HomeAddressID = c.HomeAddress.AddressID,
                WorkAddressID = c.WorkAddress.AddressID
            })
            .ToTable("Client");
    }
}

So then in my Controller I can return the following as my Model:

Context.Clients.Take(10).OrderBy(i => i.Name)

Which returns 10 results from the database as expected, apart from that it returns a default Address object for Client.WorkAddress and Client.HomeAddress.

My guess is that either I'm setting ObjectContext.ContextOptions.LazyLoadingEnabled = true in the wrong place, or (more likely) I'm not getting my relationships between Client and Address correct.

1 Answer 1

8

Your address properties aren't virtual. Lazy loading can't work on a POCO unless the properties are virtual.

Sign up to request clarification or add additional context in comments.

1 Comment

Craig. Thanks for that - yes all collections in your POCO classes must be virtual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.