类的属性,在数据库包含它时为null

时间:2015-02-16 13:50:16

标签: c# entity-framework ef-code-first

我有这两个模型:

public class SystemGroup
{
    public Guid ID { get; set; }
    public string Name { get; set; }
    public virtual SystemGroup ParentGroup { get; set; }
    public virtual ICollection<SystemGroup> SubGroups { get; set; }
    public virtual ICollection<SystemGroupItem> Items { get; set; }
}

public class SystemGroupItem
{
    public Guid ID { get; set; }
    public string Name { get; set; }
    public SystemGroup Group { get; set; }
    public ICollection<Person> AppliedPersons { get; set; }
}

问题在于此代码

YarigaranDbContext context = new YarigaranDbContext();
SystemGroupItem i = context.SystemGroupItems.First();
return View(Items);

i.Groupnull,而我在我的数据库中有{{1}}:

screenshot of table in database

我使用EF版本6

1 个答案:

答案 0 :(得分:1)

确保您的媒体资源为virtual