Entity Framework adding item to ICollection error

.net c# ef-code-first entity-framework

Question

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 = "jd@email.com";
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?

1
3
6/7/2013 10:11:12 AM

Accepted Answer

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; }
}
5
6/7/2013 10:27:42 AM

Popular Answer

try this. just initialize Follower

var db = new MyContext();

var user = new User();
user.Name = "John Doe";
user.Email = "jd@email.com";
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(); 


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow