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 6717033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:48:17+00:00 2026-05-26T08:48:17+00:00

As far as I did research on defining many to many relations with Code

  • 0

As far as I did research on defining many to many relations with Code First, I guess that custom constructors in the entity classes are only needed for the purpose of being able to create a new instance of an entity plus the n:m-related entity AT ONCE.

At the moment I have my classes defined like this:

public class Person
{
    public Person()
    {
        Events = new HashSet<Event>();
    }

    public int PersonId { get; set; }
    public virtual ICollection<Event> Events { get; set; }
}

public class Event
{
    public Event()
    {
        Persons = new HashSet<Person>();
    }

    public int EventId { get; set; }
    public virtual ICollection<Person> Persons { get; set; }
}

However, if my application will never offer the possibility to create a new Person during creating a new Event, can I simply omit the custom constructor for Events?

public class Event
{   
    public int EventId { get; set; }
    public virtual ICollection<Person> Persons { get; set; }
}

Will the many to many relation still work fine?

  • 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-26T08:48:17+00:00Added an answer on May 26, 2026 at 8:48 am

    If you do that you’ll get a NullReferenceException when you create a new event and try to add Persons to it.

    var @event = new Event();
    event.Persons.Add(new Person()); //NullReferenceException here
    

    that is the only reason for that constructor, to initialise the collections.

    you can initialise the Persons collection lazily inside the getter on first access but you need to be careful with multithreading.

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

Sidebar

Related Questions

I am providing u the sample code that i have did so far. What
I've done this before once, I'm trying to replicate what I did so far
Recently I had to develop several web applications. I did extensive research regarding JavaScript
So far I did not like EF. Although I liked the tools and how
I did my research on how to implement comet like chat on asp.net /
I already did a lot of research on this topic and have implemented a
Far as best practices are concerned, which is better: public void SomeMethod(string str) {
As far as I know, foreign keys (FK) are used to aid the programmer
So far I have encountered adjacency list, nested sets and nested intervals as models
As far as I know, Flash has to pass info off to another external

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.