Having
public class ClassOne
{
[Key]
public virtual int ClassOneID { get; set; }
[InverseProperty("ClassOne")]
public virtual ICollection<ClassTwo> ClassTwoCollection { get; set; }
}
public class ClassTwo
{
[Key]
public virtual int ClassTwoID { get; set; }
[ForeignKey("ClassOne")]
public virtual int ClassOneID { get; set; }
[InverseProperty("ClassTwoCollection")]
public virtual ClassOne ClassOne { get; set; }
[InverseProperty("ClassTwo")]
public virtual ICollection<ClassThree> ClassThreeCollection { get; set; }
[InverseProperty("ClassTwo")]
public virtual ClassFour ClassFour { get; set; }
[InverseProperty("ClassTwoPrimary")]
public virtual ICollection<ClassFour> ClassFourCollectionAsPrimary { get; set; }
}
public class ClassThree
{
[Key]
public virtual int ClassThreeID { get; set; }
[ForeignKey("ClassTwo")]
public virtual int ClassTwoID { get; set; }
[InverseProperty("ClassThreeCollection")]
public virtual ClassTwo ClassTwo { get; set; }
}
public class ClassFour
{
[Key]
[ForeignKey("ClassTwo")]
public virtual int ClassTwoID { get; set; }
[ForeignKey("ClassTwoPrimary")]
public virtual int ClassTwoPrimaryID { get; set; }
[InverseProperty("ClassFour")]
public virtual ClassTwo ClassTwo { get; set; }
[InverseProperty("ClassFourCollectionAsPrimary")]
public virtual ClassTwo ClassTwoPrimary { get; set; }
}
If I create a ClassOne: myDbCntxt.ClassOneSet.Create(); the ClassTwoCollection is initialized.
If I create a ClassTwo: myDbCntxt.ClassTwoSet.Create(); the ClassThreeCollection and ClassFourCollectionAsPrimary are null.
WHY?
Proxy creation is enabled and I can see the class name generated an they are proxy classes.
I know I’m over decorating my properties, but that is not the question today.
All I would like to understand is WHY for ClassTwo the Collection properties are not initialized when created from DbContext.
Thanks
UPDATE:
The problem was actually having another member NOT declared as virtual in ClassTwo. I guess there is the rule that all your public members should be virtual for the collection properties to be initialized in the proxies.
Found the problem.
The problem was actually having another member NOT declared as virtual in ClassTwo.
I guess there is the rule that all your public members should be virtual for the collection properties to be initialized in the proxies.
I really don’t understand why this behavior. What if you don’t want an Int32 property to be overriden at all in your proxies. This shouldn’t affect the default behavior for virtual navigation properties.