Philip Wade
Philip Wade

Reputation: 358

Entity Framework adding item to ICollection error

I have two simple classes, User and Task:

class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

class Task
{
    public int TaskId { get; set; }
    public string Name { get; set; }
    public DateTime CreatedAt { get; set; }
    public virtual ICollection<User> Followers { get; set; }
}

The Task class has a property Followers which is an ICollection<User>

Here is the db context class:

class MyContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Task> Tasks { get; set; }

        protected override void OnModelCreating(DbModelBuilder mb)
        {
            mb.Entity<User>().HasKey(u => u.UserId); 
            mb.Entity<Task>().HasKey(t => t.TaskId);
        }
    }

and here is the code in the Main program:

var db = new MyContext();

var user = new User();
user.Name = "John Doe";
user.Email = "[email protected]";
db.Users.Add(user);
db.SaveChanges();

var follower = db.Users.Where(u => u.Name == "John Doe").FirstOrDefault();

var task = new Task();
task.Name = "Make the tea";
task.CreatedAt = DateTime.Now;
task.Followers.Add(follower);  // ERROR

db.Tasks.Add(task);

db.SaveChanges();

The trouble is I am getting an error when trying to add the follower to the task.

Object reference not set to an instance of an object.

What am I doing wrong?

Upvotes: 4

Views: 11507

Answers (4)

Ulyses
Ulyses

Reputation: 121

You can initialize the List, because ICollection is an interface then it can't be initialized, but List can be (the following worked for me)

Instead of:

task.Followers.Add(follower);

Write:

task.Followers= new List<User>();
task.Followers.Add(follower);

This should solve your problem :)

Upvotes: 0

Atish Kumar Dipongkor
Atish Kumar Dipongkor

Reputation: 10422

try this. just initialize Follower

var db = new MyContext();

var user = new User();
user.Name = "John Doe";
user.Email = "[email protected]";
db.Users.Add(user);
db.SaveChanges();

var follower = db.Users.Where(u => u.Name == "John Doe").FirstOrDefault();

var task = new Task();
task.Name = "Make the tea";
task.CreatedAt = DateTime.Now;
task.Followers =  new Collection<User>()
task.Followers.Add(follower);

db.Tasks.Add(task);

db.SaveChanges(); 

Upvotes: 4

Kosala W
Kosala W

Reputation: 2143

Try this. You will have to chan ge the constructor as mentioned by qujck

        var db = new MyContext();

        var user = new User();
        user.Name = "John Doe";
        user.Email = "[email protected]";

        var task = new Task();
        task.Name = "Make the tea";
        task.CreatedAt = DateTime.Now;

        task.Followers.Add(user);
        db.Tasks.Add(task);
        db.SaveChanges();

Upvotes: 0

qujck
qujck

Reputation: 14578

The problem is that the Followers collection is null. Instead of newing up your classes, let EF create them for you ...

var user = db.Users.Create();

and

var task = db.Tasks.Create();

If you're still getting problems then your proxies are not being created. You can initialise the collections in the class constructors, make each of them a HashSet<T>. It would be better though to identify why the proxies are not getting generated ...

public class Task
{
    public Task()
    {
       Followers = new HashSet<User>();
    }
    public int TaskId { get; set; }
    public string Name { get; set; }
    public DateTime CreatedAt { get; set; }
    public virtual ICollection<User> Followers { get; set; }
}

Upvotes: 7

Related Questions