Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My entity class
C#
public class person
    {
        public int PersonId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Course> courses { get; set;}


       
    }

    public class Course
    {
        public int CourseId { get; set; }
        public string Title { get; set; }


        public virtual ICollection<person> Persons { get; set; }

       

    }

My Context class code
C#
public class Context : DbContext
    {
        public Context()
            : base("test")
        { }
        public DbSet<person> Persons { get; set; }
        public DbSet<Course> Courses { get; set; }

 modelBuilder.Entity<Course>()
                .HasMany(c => c.Persons)
                .WithMany(p => p.courses)
                .Map(m =>
                {
                    m.MapLeftKey("CourseId");
                    m.MapRightKey("PersonId");
                    m.ToTable("PersonCourses");


                }
                );

When I try add Objects to course Collection which is in persons class
i am getting error like this Object reference not set to an instance of an object.

and My Code is here:

C#
using (Context db = new Context())
{
Course stoptalk = new Course();
Course ruby = new Course();
Course rails = new Course();
 
stoptalk.Title = "Stoptalk";
ruby.Title = "Ruby";
rails.Title = "Rails";
 

db.Courses.Add(stoptalk);
db.Courses.Add(ruby);
db.Courses.Add(rails);
 
person john = new person();
john.FirstName = "John";
john.LastName = "Meclian";
 

if (john.courses == null)
{
john.courses.Add(stoptalk);
john.courses.Add(rails);
db.Persons.Add(john);
db.SaveChanges();
}
 
}
Posted
Updated 26-Dec-14 1:21am
v5
Comments
King Fisher 26-Dec-14 5:07am    
Unclear. Update your Question please ...
SreerangaPrasad sane 26-Dec-14 6:18am    
@king fisher I attached My code below can u check once
King Fisher 26-Dec-14 6:25am    
that is Solution Part Update your Question With that Code
SreerangaPrasad sane 26-Dec-14 6:32am    
I updated Question once check it
King Fisher 26-Dec-14 7:00am    
Remove your Answer

1 solution

This part of the code will give you this problem
C#
if (john.courses == null)
{
    john.courses.Add(stoptalk);
    john.courses.Add(rails);
    db.Persons.Add(john);
    db.SaveChanges();
}

because if john.courses == null is true, the Add operation cannot be executed as no instance of john.courses has been created.

What you need to do is to initialize the member courses properly.

Example:
C#
public class person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual ICollection<Course> courses { get; set;}

    // Default constructor
    public person()
    {
        PersonId = 0;
        FirstName = "";
        LastName = "";
        courses = new List<Course>;
    }
}
</course>

Then change the if-statement:
C#
if (john.courses.Count == 0)
{
  ...
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900