Reputation: 161
I am using code first to an existing database, and i am having some trouble getting one of my navigation properties to lazy load. Namely the only one-to-one relationship in my codebase.
The entityes look like this:
public abstract class DomainObject<T>
{
/// <summary>
/// The Id of this object.
/// </summary>
[Key]
public T Id { get; set; }
}
[Table("Ordre")]
public class KundeOrdre : DomainObject<int>
{
//normal properties above
public virtual Bestilling Bestilling { get; set; }
//Various methods and other navigational properties below
}
[Table("Bestilling")]
public class Bestilling : DomainObject<int>
{
//normal properties above
public virtual KundeOrdre KundeOrdre { get; private set; }
//Various methods and other navigational properties below
}
And their fluent mapping looks like this:
modelBuilder.Entity<KundeOrdre>()
.HasRequired<Bestilling>(x => x.Bestilling)
.WithRequiredPrincipal(x => x.KundeOrdre)
.Map(x => x.MapKey(OrderFK));
If i eager load bestilling this seems to work as intended, but if i try to lazy load it i get back an object where all properties are null or default values.
Upvotes: 3
Views: 1152
Reputation: 161
I found a design fault in our code, the bestilling navigation property was being instantiated in the constructor. Removing that line fixed all of my issues in a single stroke.
Upvotes: 3