0

I'm trying to select a column from a SQL database using Entity Framework.

I have declared the entity at the top

public static BoWEntity bow;

and I'm trying to select all from a column and put it into a list

var list = bow.users.Select(z => z.username).Distinct().ToList();

However, I get an error

Object reference not set to an instance of an object

My SQL table is not empty, and Entity Framework is properly reflecting the database.

I don't understand why it would give me this error and say its null?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
h3x3n95
  • 37
  • 5

1 Answers1

3

Where's the code that assigns a value to bow?

It should be

bow = new BoWEntity();

You can put this in your constructor. It is not a great idea to have a static reference to your entities in EF because of issues of scope and transactions. You're better off creating it when you need it, or injecting it into your class to use just within that class.

NibblyPig
  • 51,118
  • 72
  • 200
  • 356