ASP.NET MVC3 - “对象引用未设置为对象的实例”错误

时间:2012-01-05 10:23:28

标签: c# asp.net asp.net-mvc-3 ef-code-first

我对.NET和MVC3比较陌生。我在尝试添加对象的实例时遇到上述错误消息时遇到了一些问题。以下是我的代码:

KeyDate类

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; }
}

关联类

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; }
}

我在下面发布的控制器中收到错误

    [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");
    }

我已经google了很多年了,并且一直在关注ScottGu的“Code First with Existing Database”示例,其中代码非常相似,但我没有运气。任何帮助赞赏。感谢。

4 个答案:

答案 0 :(得分:3)

Associations为空。它尚未初始化。

KeyDate添加构造函数并在其中初始化:

public KeyDate()
{
     Associations = new List<Association>();
}

答案 1 :(得分:3)

您的Assosiactions List需要在向其添加项目之前进行实例化。

最佳做法是在构造函数中执行此操作。

public class KeyDate
{
...
    public virtual ICollection<Association> Associations { get; set; }
    public KeyDate()
    {
       Associations = new List<Association>()
    }
}

答案 2 :(得分:1)

看起来keyDate.Associations为空。

更改

keyDate.Associations.Add(keyDateTopic);

if ( keyDate.Associations == null)
{
   keyDate.Associations = new Collection<Association>();
}

keyDate.Associations.Add(keyDateTopic);

答案 3 :(得分:1)

描述

看起来keyDate.Associations为空

您有几种方法可以解决此问题

示例

  • 在ActionMethod中创建ICollection

    [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");
    }
    
  • KeyDate类中添加构造函数

    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; }
    }