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

  • SEARCH
  • Home
  • 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 9247245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:40:49+00:00 2026-06-18T09:40:49+00:00

The usual approach for modeling 1-n relations with EntityFramwork (Code First) is to use

  • 0

The usual approach for modeling 1-n relations with EntityFramwork (Code First) is to use virtual collection properties like:

class Project {
  public virtual ICollection<Remark> Remarks { get; set; }
}

class Remark {
  public virtual int ProjectId { get; set; }
  public virtual Project Project {get; set; }
}

Since the Remarks collection is initially null, I take the following approach

  private ICollection<Remark> _remarks;
  public virtual ICollection<Remark> {
    get {
      if (_remarks == null)
        _remarks = new List<Remark>();
      return _remark;
    }
    set {
       _remarks = value;
    }
  }

in order to use the Remarks.Add method on a newly created Project object without the need to explicitely set the Property.

Afaik EF internally derives from my class and overwrites the virtual navigation properties to support lazy loading.

My question: Do I need to define the setter of a collection property? Does EF require it? I’d rather like to only expose the getter and let the class manage the collection internally.

Edit Accidentially I only noticed this related question just after postin mine, so maybe it’s just a duplicate …

  • 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-06-18T09:40:50+00:00Added an answer on June 18, 2026 at 9:40 am

    Entity framework can handle private members. You can give the property a private setter:

    private ICollection<Remark> _remarks;
    public virtual ICollection<Remark> Remarks
    {
        get { return _remarks ?? (_remarks = new HashSet<Remark>()); }
        private set { _remarks = value; }
    }
    

    You can even omit the setter altogether.

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

Sidebar

Related Questions

The usual pattern for a singleton class is something like static Foo &getInst() {
The usual approach to getting an array's element count in C in something like
I have a class that acts like this at the moment: public class MyClass
The usual way to assign color box functionality on a link is like this:
The usual advice when rethrowing an exception is to use a throw; statement so
Is it usual to use a Dialog as main Windows? So without registering any
I'm trying to install EclipseFP, using the usual Eclipse Install new software approach. However,
I found next code in official java docs : collection.removeAll(Collections.singleton(element)); I couldn't figure out
I am running into a problem with webservices. My usual approach to development is
What is best practice to use and distribute 3rd party jars? Approach one: Keep

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.