mosquito87
mosquito87

Reputation: 4440

Entity Framework & lazy loading not working

I have the following model:

public sealed class Consignor : TwoNames
{
    public Consignor()
    {
        Address = new Address();
    }
}

It's mother class TwoNames looks like this:

public abstract class TwoNames : Search
{
    [Required]
    public int AddressId { get; set; }
    public virtual Address Address { get; set; }

    [Required]
    public string Name1 { get; set; }
    public string Name2 { get; set; }
}

And my Address model is here:

public class Address : Model
{
    [Required]
    public string Street { get; set; }
    [Required]
    public string ZipCode { get; set; }
    [Required]
    public string City { get; set; }
    public string Country { get; set; }
    public string Email { get; set; }
}

They all inherit from "Model". Model has just an id. Everything works well, BUT: Lazy loading seems to be not working.

I'm loading a consignor like this:

List<Consignor> consignors = UnitOfWork.ConsignorRepository.Get().ToList();

All of the consignors got the correct AddressID and Address is not null (I guess because of my constructor in the Consignor class) but the Address property is not filled correctly (no street, no zip code, etc.).

Upvotes: 0

Views: 103

Answers (1)

Jason Hischier
Jason Hischier

Reputation: 130

It looks like the Consignor's constructor will instantiate a new Address object with blank street, zip, etc. (like you mentioned in your last paragraph); if you remove the instantiation of Address, you should end up with a not-loaded Address on your Consignor objects that you can lazily load as needed.

Upvotes: 1

Related Questions