0

I am new to Entity Framework. Just wondering what is the right way to create related entities with many-to-many relationships. I always get this error when debugging: "Object reference not set to an instance of an object".

Take the following as an example:

Below is the data model, a Club can have multiple Members, a Member can join multiple clubs

public class Club       
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Member> Member { get; set; }
}
public class Member
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Club> Club { get; set; }
}

I tried to create a new Club and add a new Member to it (Neither object is existing data in the DB):

Member member = new Member();
Club club=new Club();
member.Name = "Amy";
club.Name="NBA Fans";
member.Club.Add(club); //Error: Object reference not set to an instance of an object
db.Member.Add(member);
db.SaveChanges();

Can anyone please provide the right way to do this?

Thanks a lot

Michael
  • 71
  • 1
  • 5
  • Have you tried looking for examples? Like this one *[Configure Many-to-Many relationship using Code First Approach](http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx)* – Richard Nov 07 '15 at 09:14
  • Yes, I have read quite a few tutorials on the web. But I did not identify a tutorial on creating related entities with many-to-many relationships. – Michael Nov 07 '15 at 09:18
  • I am not sure if entity framework supports creating two new entities with many-to-many relationships in one query, or this has to be done in the following way: create a club first, retrieve it from the db, create a member, link the club to the new member. – Michael Nov 07 '15 at 09:23

1 Answers1

0

You can try this

Edit :

Follow this link Saving Many to Many relationship data on MVC Create view

public class Club       
{
    public int ClubId { get; set; }
    public string Name { get; set; }
}
public class Member
{
    public int MemberId { get; set; }
    public string Name { get; set; }
}

Edit Class :

public class ClubMembers
{
    public int ClubMember { get; set; }
    public List<Member> Member { get; set; }
    public List<Club> Club { get; set; }
}

List<Club> clubList=new List<Club>();
Club club = new Club();
club.ClubId=1;
club.Name="Club1";
clubList.Add(club);

List<Member> memberList = new List<Member>();
Member member = new Member();
member.MemberId = 1;
member.Name = "Govinda";
memberList.Add(member);

ClubMembers clubMembers=new ClubMembers();
clubMembers.Club = clubList;
clubMembers.Member=memberList;

foreach (var item in clubMembers.Club)
{
    //do code for club entity
}

foreach (var item in clubMembers.Member)
{
    //do code for member entity
}
Community
  • 1
  • 1
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
  • Thanks for the idea, but it seems to me that the data model logic is not quite right. – Michael Nov 07 '15 at 09:20
  • Thanks for the improvement. Your code is quite different from my initial idea. What I wanted to achieve is that one person can be a member of multiple clubs and of course one club can have multiple members. But your code lost this relationship. For example, how to search list of members for a club with your logic? Thanks – Michael Nov 07 '15 at 09:34
  • It is easy to do. you can create a new table which contains 3 columns as mention in `ClubMembers` class and than after you can easily find something "DbContext.ClubMembers.where(x => x.Club.ClubID == clubId).tolList();" it will return you list of ClubMembers so from it you can archive your goal. – Govinda Rajbhar Nov 07 '15 at 09:42
  • Sorry, I tried it. It seems to me that the logic for constructing many-to-may relationship in entity framework is not right according to many tutorials from Microsoft and others. – Michael Nov 07 '15 at 09:50