Reputation: 751
I'm relatively new to .NET and MVC3. I'm having some trouble with the above error message when trying to add an instance of an object. Below is my code:
KeyDate class
public class KeyDate
{
[Key]
public int KeyID { get; set; }
[StringLength(1000), Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[Required]
[Display(Name = "Event Date")]
public DateTime EventDate { get; set; }
[Display(Name = "Event End Date")]
public DateTime? EventEndDate { get; set; }
[Display(Name = "Course ID")]
public int? CourseID { get; set; }
[Display(Name = "Event ID")]
public int? EventID { get; set; }
public DateTime Created { get; set; }
[Display(Name = "Created By")]
public string CreatedBy { get; set; }
public DateTime? Deleted { get; set; }
[Display(Name = "Deleted By")]
public string DeletedBy { get; set; }
public virtual ICollection<Association> Associations { get; set; }
}
Association class
public class Association
{
[Key, ForeignKey("KeyDate")]
[Column(Order = 1)]
public int KeyID { get; set; }
[Key, ForeignKey("Category")]
[Column(Order = 2)]
public int CategoryID { get; set; }
public virtual KeyDate KeyDate { get; set; }
public virtual Category Category { get; set; }
}
I am getting the error in my Controller posted below
[HttpPost]
public ActionResult Create(KeyDate keyDate, int topic)
{
keyDate.Created = DateTime.Now;
keyDate.CreatedBy = User.Identity.Name;
// Create topic association
Association keyDateTopic = new Association();
keyDateTopic.CategoryID = topic;
keyDate.Associations.Add(keyDateTopic); // <-- Object reference... error here
repository.Add(keyDate);
repository.Save();
return RedirectToAction("Index");
}
I have Googled this for ages, and have been following ScottGu's "Code First with Existing Database" example in which the code is quite similar, but I have had no luck. Any help appreciated. Thanks.
Upvotes: 3
Views: 6151
Reputation: 60506
It looks like keyDate.Associations
is null
You have several ways to solve this
Create the ICollection
in your ActionMethod.
[HttpPost]
public ActionResult Create(KeyDate keyDate, int topic)
{
keyDate.Created = DateTime.Now;
keyDate.CreatedBy = User.Identity.Name;
// Create topic association
Association keyDateTopic = new Association();
keyDateTopic.CategoryID = topic;
// create this list
keyDate.Associations = new List<Association>();
keyDate.Associations.Add(keyDateTopic); // <-- Object reference... error here
repository.Add(keyDate);
repository.Save();
return RedirectToAction("Index");
}
OR Add a constructor to your KeyDate
class like this
public class KeyDate
{
public KeyDate()
{
// create this list
this.Associations = new List<Association>();
}
[Key]
public int KeyID { get; set; }
[StringLength(1000), Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[Required]
[Display(Name = "Event Date")]
public DateTime EventDate { get; set; }
[Display(Name = "Event End Date")]
public DateTime? EventEndDate { get; set; }
[Display(Name = "Course ID")]
public int? CourseID { get; set; }
[Display(Name = "Event ID")]
public int? EventID { get; set; }
public DateTime Created { get; set; }
[Display(Name = "Created By")]
public string CreatedBy { get; set; }
public DateTime? Deleted { get; set; }
[Display(Name = "Deleted By")]
public string DeletedBy { get; set; }
public virtual ICollection<Association> Associations { get; set; }
}
Upvotes: 1
Reputation: 7369
Your Assosiactions List needs to be instantiated before adding items to it.
Best practice is to do this from within the constructor.
public class KeyDate
{
...
public virtual ICollection<Association> Associations { get; set; }
public KeyDate()
{
Associations = new List<Association>()
}
}
Upvotes: 3
Reputation: 81670
Associations
is null. It has not been initialised.
Add a constructor for KeyDate
and init it in there:
public KeyDate()
{
Associations = new List<Association>();
}
Upvotes: 3
Reputation: 62544
Looks like keyDate.Associations
is null.
Change
keyDate.Associations.Add(keyDateTopic);
to
if ( keyDate.Associations == null)
{
keyDate.Associations = new Collection<Association>();
}
keyDate.Associations.Add(keyDateTopic);
Upvotes: 1