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.
Associations
is null. It has not been initialised.
Add a constructor for KeyDate
and init it in there:
public KeyDate()
{
Associations = new List<Association>();
}
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>()
}
}
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);
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; } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.