Asp.net mvc 2 实体框架CTP4建模和映射问题/问题
我有一个网站,允许用户在主网站上创建尽可能多的微网站。用户还可以将其他用户设置为其微站点的版主,用户可以关注其他微站点。我不确定我做错了什么(这似乎适用于CTP3),但当我尝试做一些简单的事情时,比如Asp.net mvc 2 实体框架CTP4建模和映射问题/问题,asp.net-mvc-2,c#-4.0,domain-driven-design,entity-framework-4,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); 我得到一个系统.NullReferenceException:对象引用未设置为对象的实例 这就是我的实体的外观以及映射它们
var site = _siteRepo.SingleOrDetault(x => x.Id == id);
site.Moderators.Add(user1);
我得到一个系统.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");
}
公共类用户:IEntity
{
私有日期时间_createdAt;
private DateTime\u lastActivityAt;
公共用户()
{
_createdAt=SystemTime.Now();
_lastActivityAt=SystemTime.Now();
}
公共虚拟长Id{get;private set;}
公共虚拟字符串电子邮件{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟字符串密码哈希{get;set;}
公共虚拟字符串PasswordSalt{get;set;}
公共虚拟bool已确认{get;set;}
公共虚拟ICollection主持人{get;set;}
在{get;set;}之后的公共虚拟ICollection
公共虚拟ICollection站点{get;set;}
公共虚拟日期时间LastActivityAt
{
[调试步骤至]
获取{return\u lastActivityAt;}
[调试步骤至]
设置{u lastActivityAt=value;}
}
公共虚拟日期时间创建数据
{
[调试步骤至]
获取{return\u createdAt;}
[调试步骤至]
设置{u createdAt=value;}
}
公共角色
{
获取{return(Role)InternalRole;}
设置{InternalRole=(int)值;}
}
[EditorBrowsable(EditorBrowsableState.Never)]
公共虚拟角色
{
[EditorBrowsable(EditorBrowsableState.Never)]
得到;
[EditorBrowsable(EditorBrowsableState.Never)]
设置
}
}
公共课堂网站:IEntity
{
私有日期时间_createdAt;
公共网站()
{
_createdAt=SystemTime.Now();
}
公共虚拟长Id{get;set;}
公共虚拟字符串Slug{get;set;}
公共虚拟bool IsPrivate{get;set;}
公共虚拟布尔是活动的{get;set;}
公共虚拟布尔值受管理限制{get;set;}
公共虚拟bool HasDonationPage{get;set;}
公共虚拟站点数据站点数据{get;set;}
公共虚拟主题{get;set;}
公共虚拟用户用户{get;set;}
公共虚拟ICollection版主{get;set;}
在{get;set;}之后的公共虚拟ICollection
公共虚拟ICollection站点公告{get;set;}
公共虚拟ICollection站点链接{get;set;}
公共虚拟ICollection站点帖子{get;set;}
公共虚拟站点捐赠站点捐赠{get;set;}
公共虚拟日期时间创建数据
{
获取{return\u createdAt;}
设置{u createdAt=value;}
}
}
公共用户映射()
{
HasKey(u=>u.Id);
属性(u=>u.Id).IsIdentity();
属性(u=>u.Name).IsRequired().IsVariableLength().HasMaxLength(75);
属性(u=>u.Email).IsRequired().IsVariableLength().HasMaxLength(75);
属性(u=>u.PasswordHash).IsRequired().IsVariableLength().HasMaxLength(215);
属性(u=>u.PasswordSalt).IsRequired().IsVariableLength().HasMaxLength(88);
属性(u=>u.isconfirm);
属性(u=>u.LastActivityAt);
属性(u=>u.CreatedAt);
属性(u=>u.InternalRole);
MapSingleType(u=>new
{
u、 身份证,
u、 名字,
u、 电子邮件,
u、 密码哈希,
u、 密码盐,
u、 我确认,
角色=美国内部角色,
u、 最近的活动,
u、 创建数据
}).ToTable(“用户”);
}
公共网站地图()
{
HasKey(s=>s.Id);
属性(s=>s.Id).IsIdentity();
属性(s=>s.HasDonationPage);
属性(s=>s.IsActive);
属性(s=>s.IsAdminRestricted);
属性(s=>s.IsPrivate);
属性(s=>s.Slug.IsRequired().IsVariableLength().HasMaxLength(125);
属性(s=>s.CreatedAt);
MapSingleType=>new
{
s、 身份证,
ThemeId=s.Theme.Id,
UserID=s.User.Id,
s、 HasDonationPage,
s、 我很活跃,
s、 我受到限制,
s、 我个人,
s、 鼻涕虫,
s、 创建数据
}).ToTable(“网站”);
}
在向ICollection添加对象之前,需要对其属性进行null检查
或在构造函数中设置默认值:
public User()
{
_createdAt = SystemTime.Now();
_lastActivityAt = SystemTime.Now();
Moderator = new List<Site>();
Following = new List<Site>();
Sites = new List<Site>();
}
公共用户()
{
_createdAt=SystemTime.Now();
_lastActivityAt=SystemTime.Now();
主持人=新列表();
Following=新列表();
站点=新列表();
}