实体框架CTP4建模和映射问题/问题

时间:2010-09-06 18:27:55

标签: asp.net-mvc-2 c#-4.0 domain-driven-design entity-framework-4

我有一个网站,允许用户根据自己的喜好在主网站上创建尽可能多的微网站。用户还可以将其他用户设置为其微站点的主持人,用户可以关注其他微站点。我不确定我做错了什么(这似乎适用于CTP3),但当我尝试做简单的事情,如

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

site.Moderators.Add(user1);

我得到一个System.NullReferenceException:对象引用未设置为对象的实例。

这就是我的实体的样子以及我如何映射它们。

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

1 个答案:

答案 0 :(得分:0)

在向对象添加对象之前,需要对ICollection的属性进行null检查。

或者在构造函数中设置默认值:

public User()
        {
            _createdAt = SystemTime.Now();
            _lastActivityAt = SystemTime.Now();
            Moderator = new List<Site>();
            Following = new List<Site>();
            Sites = new List<Site>();
        }