带有linq的C#Entity Framework返回null引用

use*_*072 7 c# linq frameworks entity

我在C#中遇到了实体框架的问题.我有2个实体,User和UserRole.他们是关系用户* - > 1 UserRole

每当我在函数中使用此查询时:

User user = context.User.Where(i => i.id == id).FirstOrDefault();
return user.UserRole.accessLevel;
Run Code Online (Sandbox Code Playgroud)

查询返回用户,但UserRole为null.User表具有与UserRole的id相关的roleId,以及在调试正确时roleId的值,尽管UserRole实体为null.这很奇怪,因为它从未发生过......

我已经确定我在模型和数据库中的关系是正确的.我有正确的行添加到数据库.

编辑:

对不起,我应该提到我使用自定义可测试数据库控制器:

public class DBController : IUnitOfWork
{
    readonly ObjectContext context;
    const string ConnectionStringName = "MarketPlaceDBEntities";

    public DBController()
    {
        var connectionString =
        ConfigurationManager
            .ConnectionStrings[ConnectionStringName]
            .ConnectionString;
        context = new ObjectContext(connectionString);
    }

    public void Commit()
    {
        context.SaveChanges();
    }

    public IObjectSet<Category> Category
    {
        get { return context.CreateObjectSet<Category>(); }
    }
    public IObjectSet<ItemComment> ItemComment 
    {
        get { return context.CreateObjectSet<ItemComment>(); }
    }
    public IObjectSet<ItemRating> ItemRating 
    {
        get { return context.CreateObjectSet<ItemRating>(); }
    }
    public IObjectSet<Item> Item 
    {
        get { return context.CreateObjectSet<Item>(); }
    }
    public IObjectSet<ItemSale> ItemSale 
    {
        get { return context.CreateObjectSet<ItemSale>(); }
    }
    public IObjectSet<ItemScreenshot> ItemScreenshot 
    {
        get { return context.CreateObjectSet<ItemScreenshot>(); }
    }
    public IObjectSet<UserRole> UserRole 
    {
        get { return context.CreateObjectSet<UserRole>(); }
    }
    public IObjectSet<User> User 
    {
        get { return context.CreateObjectSet<User>(); }
    }
}
Run Code Online (Sandbox Code Playgroud)

我通过它做手术.也许它必须对我的概率做些什么.

interface IUnitOfWork
{
    IObjectSet<Category> Category { get; }
    IObjectSet<ItemComment> ItemComment { get; }
    IObjectSet<ItemRating> ItemRating { get; }
    IObjectSet<Item> Item { get; }
    IObjectSet<ItemSale> ItemSale { get; }
    IObjectSet<ItemScreenshot> ItemScreenshot { get; }
    IObjectSet<UserRole> UserRole { get; }
    IObjectSet<User> User { get; }
    void Commit();
}
Run Code Online (Sandbox Code Playgroud)

我以前做过这整件事,但不知道为什么会出错.

EDIT2:

解决了!谢谢RicoSuter.

在我的db控制器的构造函数中启用延迟加载解决了这个问题.我认为它已经启用,因为它在数据库模型中设置为true,但在创建新上下文时,您必须再次手动启用它.

public DBController()
{
    var connectionString =
    ConfigurationManager
        .ConnectionStrings[ConnectionStringName]
        .ConnectionString;
    context = new ObjectContext(connectionString);
    context.ContextOptions.LazyLoadingEnabled = true;
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ter 7

尝试热切地加载UserRole(加入):

context.User.Include("UserRole").Where(i => i.id == id).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

或首先启用延迟加载:

context.ContextOptions.LazyLoadingEnabled = true;
context.User.Where(i => i.id == id).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

否则与UserRole数据库中的a无关...