Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6743083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:55:10+00:00 2026-05-26T11:55:10+00:00

Having public class ClassOne { [Key] public virtual int ClassOneID { get; set; }

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:55:10+00:00Added an answer on May 26, 2026 at 11:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the following interfaces: public interface IBase { int id1 { get; set;
Having the following class: public class SomeClass { private readonly int[] _someThings; public SomeClass()
Having the following class (.Net 3.5): public class Something { public string Text {get;
Having this model class public class Usuario { #region Atributos private int _intID =
The class to instantiate: public class InstantiateMe { public String foo { get; set;
Having something like this this : public abstract class AAA { protected abstract virtual
I'm having trouble using my own class as a key for a HashMap public
Having the following classes, in two different assemblies: class Member { public string Label
i am having a strange problem, i have a static class like this public
I'm having trouble understanding the following syntax: public class SortedList< T extends Comparable< ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.