代码之家  ›  专栏  ›  技术社区  ›  Paul

实体框架CTP4建模与映射问题

  •  0
  • Paul  · 技术社区  · 16 年前

    我有一个网站,用户可以创建尽可能多的微网站与主网站,因为他们喜欢。用户还可以将其他用户设置为其微站点的版主,用户可以关注其他微站点。我不确定我做错了什么(这似乎对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 回复  |  直到 16 年前
        1
  •  0
  •   Paul    15 年前

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

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

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