views:

60

answers:

1

I have a site where users are allowed to create as many micro-sites with in the main site as they like. A user can also set other users as moderators of their micro-site and users can follow other micro-site. I am not sure what I am doing wrong, (this seemed to work with CTP3), but when I try to do simple stuff like

var site = _siteRepo.SingleOrDetault(x => x.Id == id);

site.Moderators.Add(user1);

I get an System.NullReferenceException: Object reference not set to an instance of an object.

This is what my entities look like and how I mapped them.

 public class User : IEntity
    {
        private DateTime _createdAt;
        private DateTime _lastActivityAt;
        public User()
        {
            _createdAt = SystemTime.Now();
            _lastActivityAt = SystemTime.Now();
        }

        public virtual long Id { get; private set; }
        public virtual string Email { get; set; }
        public virtual string Name { get; set; }
        public virtual string PasswordHash { get; set; }
        public virtual string PasswordSalt { get; set; }
        public virtual bool IsConfirmed { get; set; }
        public virtual ICollection<Site> Moderator { get; set; }
        public virtual ICollection<Site> Following { get; set; }
        public virtual ICollection<Site> Sites { get; set; }
        public virtual DateTime LastActivityAt
        {
            [DebuggerStepThrough]
            get { return _lastActivityAt; }
            [DebuggerStepThrough]
            set { _lastActivityAt = value; }
        }
        public virtual DateTime CreatedAt
        {
            [DebuggerStepThrough]
            get { return _createdAt; }
            [DebuggerStepThrough]
            set { _createdAt = value; }
        }

        public Role Role
        {
            get { return (Role) InternalRole; }
            set { InternalRole = (int) value; }
        }
        [EditorBrowsable(EditorBrowsableState.Never)]
        public virtual int InternalRole
        {
            [EditorBrowsable(EditorBrowsableState.Never)]
            get;
            [EditorBrowsable(EditorBrowsableState.Never)]
            set;
        }
    }

public class Site : IEntity
    {
        private DateTime _createdAt;

        public Site()
        {
            _createdAt = SystemTime.Now();
        }
        public virtual long Id { get; set; }
        public virtual string Slug { get; set; }
        public virtual bool IsPrivate { get; set; }
        public virtual bool IsActive { get; set; }
        public virtual bool IsAdminRestricted { get; set; }
        public virtual bool HasDonationPage { get; set; }
        public virtual SiteData SiteData { get; set; }
        public virtual Theme Theme { get; set; }
        public virtual User User { get; set; }
        public virtual ICollection<User> Moderators { get; set; }
        public virtual ICollection<User> Following { get; set; }
        public virtual ICollection<SiteAnnouncement> SiteAnnouncements { get; set; }
        public virtual ICollection<SiteLink> SiteLinks { get; set; }
        public virtual ICollection<SitePost> SitePosts { get; set; }
        public virtual SiteDonation SiteDonation { get; set; }
        public virtual DateTime CreatedAt
        {
            get { return _createdAt; }
            set { _createdAt = value; }
        }

    }


        public UserMap()
        {
            HasKey(u => u.Id);
            Property(u => u.Id).IsIdentity();
            Property(u => u.Name).IsRequired().IsVariableLength().HasMaxLength(75);
            Property(u => u.Email).IsRequired().IsVariableLength().HasMaxLength(75);
            Property(u => u.PasswordHash).IsRequired().IsVariableLength().HasMaxLength(215);
            Property(u => u.PasswordSalt).IsRequired().IsVariableLength().HasMaxLength(88);
            Property(u => u.IsConfirmed);
            Property(u => u.LastActivityAt);
            Property(u => u.CreatedAt);
            Property(u => u.InternalRole);
            MapSingleType(u => new
                                   {
                                       u.Id,
                                       u.Name,
                                       u.Email,
                                       u.PasswordHash,
                                       u.PasswordSalt,
                                       u.IsConfirmed,
                                       Role = u.InternalRole,
                                       u.LastActivityAt,
                                       u.CreatedAt
                                   }).ToTable("User");
        }

        public SiteMap()
        {
            HasKey(s => s.Id);
            Property(s => s.Id).IsIdentity();
            Property(s => s.HasDonationPage);
            Property(s => s.IsActive);
            Property(s => s.IsAdminRestricted);
            Property(s => s.IsPrivate);
            Property(s => s.Slug).IsRequired().IsVariableLength().HasMaxLength(125);
            Property(s => s.CreatedAt);
            MapSingleType(s => new
                                   {
                                       s.Id,
                                       ThemeId = s.Theme.Id,
                                       UserID = s.User.Id,
                                       s.HasDonationPage,
                                       s.IsActive,
                                       s.IsAdminRestricted,
                                       s.IsPrivate,
                                       s.Slug,
                                       s.CreatedAt
                                   }).ToTable("Sites");
        }
A: 

Need to null check property of ICollection before adding objects to it.

Paul