实体框架:组创建者为null但不是吗?

时间:2014-07-04 21:44:02

标签: c# asp.net-mvc asp.net-mvc-5 entity-framework-6

这是我的代码抓取组创建者名称:

Group grp = uow.GroupRepository.Get(u => u.GroupId == groupId).First();
string creator = grp.creator.UserName.ToString();

小组课程:

public class Group
    {
        public int GroupId { get; set; }
        public Group() { this.Members = new HashSet<ApplicationUser>(); this.LockedUsers = new HashSet<string>(); this.active = true; this.Locked = true; this.LockedUntil = DateTime.Now; }
        public virtual string GroupName { get; set; }
        public ApplicationUser creator { get; set; }
        public virtual ICollection<ApplicationUser> Members { get; set; }
        public virtual ICollection<string> LockedUsers { get; set; }
        public bool active { get; set; }
        public bool Locked { get; set; }
        public DateTime LockedUntil { get; set; }
    } 

好的,所以当一个事件到达第一行时,它会使用工作单元对象来获取该组。当获取任何其他变量(例如

)时,这在调试中工作正常
string groupname = grp.GroupName();

所以,这是一个奇怪的部分。我运行代码,我得到一个NULL异常,视觉工作室坐在那里给我一些厚颜无耻的借口。我打开数据库,查看所有组数据,一切都在那里。然后我将鼠标悬停在组对象上,突然它正在注册一个组创建者....不再说它为空。我不知道为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

好的,答案是“在创作者面前添加虚拟”。如果您阅读此内容并了解为什么请编辑此内容并进行更新。非常感谢@ilanS