0

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.).

1 Answer 1

1

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.

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

2 Comments

Absolutely right. This made me revisit this answer.
That's right. I removed the constructor completely. Now when I load an consignor Address is always null and can't be accessed (NullPointerException) ...

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.