Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Entity Framework with linq returns null reference

I have a problem with entity framework in C#. I have 2 entities, User and UserRole. They are bond by relationships User *->1 UserRole

Whenever I use this query in a function:

User user = context.User.Where(i => i.id == id).FirstOrDefault();
return user.UserRole.accessLevel;

The query returns user, but UserRole is null. The User table has roleId which is related to id of UserRole, and the value of roleId when debugging is correct, although UserRole entity is null. This is strange as it never happened before...

I already made sure that my relationships in model and database are correct. I have correct rows added to database.

EDIT:

Sorry, I should've mentioned I use custom testable database controller:

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>(); }
    }
}

And I do operations via it. Maybe it has to do something with my prob.

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();
}

I had this whole thing working before, but don't know why it went wrong..

EDIT2:

Solved! Thanks RicoSuter.

Enabling lazy loading in constructor of my db controller solved the problem. I thought it was already enabled, because it was set to true in database model, but it looks like that when creating a new context, you have to enable it manually again.

public DBController()
{
    var connectionString =
    ConfigurationManager
        .ConnectionStrings[ConnectionStringName]
        .ConnectionString;
    context = new ObjectContext(connectionString);
    context.ContextOptions.LazyLoadingEnabled = true;
}
like image 981
user1548072 Avatar asked Jul 24 '12 10:07

user1548072


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

try to eagerly load UserRole (join):

context.User.Include("UserRole").Where(i => i.id == id).FirstOrDefault();

or enable lazy loading first:

context.ContextOptions.LazyLoadingEnabled = true;
context.User.Where(i => i.id == id).FirstOrDefault();

otherwise there is no relation to a UserRole in your database...

like image 173
Rico Suter Avatar answered Oct 04 '22 13:10

Rico Suter